Multikeys data structures collection written in TypeScript: Map, Set, WeakMap and WeakSet. Based on trie data structure.
Benchmarks 🚀
Benchmarks are located in ./benchmark
directory. You can modify ./benchmark/index.js
and run npm i && npm start
to test your own use cases.
Results of calling set
and get
with random data (and random number of keys). You can find benchmark code in ./benchmark/index.js
.
package | set and get (ops/sec) | normalized on many-keys-map |
---|---|---|
multikeymap | 1.37 ±19.20% | 0 |
many-keys-map | 27 795 ±0.84% | 1 |
multikey | 79 355 ±1.37% | 2.85 |
multikey-map | 153 616 ±1.32% | 5.52 |
multikeys | 185 565 ±1.70% | 6.67 |
package | set and get (ops/sec) | normalized on many-keys-map |
---|---|---|
multikeymap | fall with OOM | - |
many-keys-map | 7 120 ±0.92% | 1 |
multikey | 10 830 ±1.35% | 1.52 |
multikey-map | 29 635 ±1.26% | 4.16 |
multikeys | 43 394 ±1.52% | 6.09 |
const mkMap = new MKMap();
mkMap.set([1, 2, 3], "foo");
mkMap.set([3, 2, 1], "bar");
// order of keys matters
mkMap.get([1, 2, 3]); // => 'foo'
mkMap.get([3, 2, 1]); // => 'bar'
// an argument with empty keys is also valid
mkMap.set([], "zero");
mkMap.get([]); // => 'zero'
const mkSet = new MKSet();
const obj = {};
mkSet.add([obj, 1]);
mkSet.has([{}, 1]); // => false, because {} is a new object, {} !== obj
mkSet.has([obj, 1]); // => true
Using MKMap we could simply add memoization to function with a variable number of arguments:
const { MKMap } = require("multikeys");
function memoize(func) {
const mkMap = new MKMap();
return (...args) => {
if (mkMap.has(args)) {
return mkMap.get(args);
}
const res = func(...args);
mkMap.set(args, res);
return res;
};
}
Also, we could replace MKMap
with MKWeakMap
and get memoize
with auto garbage collection. In such case only objects could be func
arguments.