JavaScript Hashset is an open-source, pure JS implementation of the HashSet datatype.
JavaScript Hashset has 'close to universal' JS compatibility in both the browser and also in Node.js
Internet Explorer 5, IE5.5, IE6, IE7, IE8, IE9, IE10, IE11. Node, NPM, Chrome. FireFox, Opera, Konqueror.
A Hash-set is a like an array - but it differs in key 3 ways:
- It is not ordered
- It holds only values (not key-value pairs)
- Looking up values in a HashSet does not take significantly more time, even as the HashSet grows.
Short answer: because arrays get much slower to search as they get bigger.
Hashsets are useful for writing optimized code, particularly where looping and looking up values in an array.
When dealing with large datasets - traditional Array.IndexOf lookups in JS get very slow. The lookup speed grows with each item added, making nested loops potentially slow down in a N Squared pattern.
Hashsets look up values by hashing, and lookup times are relatively stable, even for huge data sets.
Even with just ten values in it, a HashSet is noticeably (2x) faster than a normal array. The performance increase grows exponentially with size: by the time a dataset gets to 100,000 items the hashset may be up to 500 times faster.
Installing PHPWee should only take a minute. Here are you 3 choices:
- Download this repository into your project
- Test it by running test.html
- Include hashset.min.js into the head of your page. (or into your node project)
<script src='js-hashset/hashset.min.js></script>
Create a new hashSet
var myhashset = new hashSet();
Add a value to the hashset. If the value already exists, it will be ignored. You can add any JS variable to the hashSet. A string , int, a class, a type, a function, anything!
myhashset.add("value");
Remove a value from the hashset. If the value does not already exists, there will be no effect.
myhashset.remove("value");
Returns an boolean. True if the given value already exists in the hashset.
var value_already_exists = myhashset.contains("value");
Empties the hashset of all values.
myhashset.clear();
Returns an integer count of all of the unique values in the hashSet.
var array_of_values = myhashset.values();
Returns an array or all of the unique values in the hashSet.
var array_of_values = myhashset.values();
Copies all of the unique values in the hashSet into an array;
array = myhashset.copyToArray(array);
Adds all elements from another hashset into this hasset.
myhashset.exceptWith(another_hashSet);
Removes all elements in the hasset that are contained in another HasSet.
myhashset.exceptWith(another_hashSet);
Returns true is every value on this hasSet is also contained in another HashSet.
myhashset.isSubsetOf(another_hashSet);
Allows enumeration of this hashset by an anonymous function. For more complex enumeration - you can always use myhashset.values as iterate as per a normal array.
myhashset.enumerate(function(value){alert(value)});
JavaScript HashSet is built and maintained by http://searchturbine.com as part of their 20% codeshare policy. They can be contacted at [email protected]
We developed this class for optimizing Node.js based search engine algorithms - but found it equally useful in the browser.s
This class is used within out own Node,js search engine core software. If you can make it even more stable, or even faster - we could be very happy.
- Please feel welcome to modify, fork and contribute.