es6-deep-set v1.0.10
DeepSet
This 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]) // falseThis 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]) // trueDeepSet 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, 6Filter:
const mySet = new DeepSet(1, 2, 3);
const newSet = mySet.filter((el) => el % 2 === 0);
newSet.values() // 2And reduce:
const mySet = new DeepSet(1, 2, 3);
const newValue = mySet.reduce((acc, el) => acc + el, 0); // newValue == 6If you don't give reduce an initial value, it will initially set the accumulator to a new DeepSet.
Advanced Users
As one would expect, 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.