A set for ES6 JavaScript which guarantees uniqueness by value instead of by reference.
npm install es6-deep-setThis library provides a class called DeepSet which guarantees uniqueness by value. Consider this classic gotcha:
```
const mySet = new Set();
mySet.add([1, 2])
mySet.has([1, 2]) // false
This unintuitive behavior occurs because the built-in Set compares the two arrays by checking their memory locations instead of their contents.
By installing this module, you get a class called DeepSet which does the more intuitive thing:
``
const mySet = new DeepSet();
mySet.add([1, 2])
mySet.has([1, 2]) // true
DeepSet has exactly the same methods as an ES6 set. It also has map:
``
const mySet = new DeepSet(1, 2, 3);
const newSet = mySet.map((el) => 2 * el);
newSet.values() // 2, 4, 6
Filter:
``
const mySet = new DeepSet(1, 2, 3);
const newSet = mySet.filter((el) => el % 2 === 0);
newSet.values() // 2
And reduce:
``
const mySet = new DeepSet(1, 2, 3);
const newValue = mySet.reduce((acc, el) => acc + el, 0); // newValue == 6
If you don't give reduce an initial value, it will initially set the accumulator to a new DeepSet.
is much slower than an ES6 Set. The main
bottleneck is its internal hashing function. If you need to swap out
that function for the sake of performance, you can do so on a
per-instance basis by changing instance.__hashingFunction__ to
something optimized for your data. Apply this technique with caution.You can run tests with
npm test and benchmarks with npm run benchmark`. Enjoy.