Multikeys is a TypeScript collection of trie‑based Map, Set, WeakMap, and WeakSet designed for high performance.
- ✨ 100% TypeScript
- 🚀 Trie‑based implementation 2x-5x faster than other libs (check benchmarks)
- 🪶 Zero dependencies
Check the full API Reference.
With npm:
npm add multikeysWith pnpm:
pnpm add multikeysimport { MKMap } from "multikeys";
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]); // => trueUsing MKMap we could simply add memoization to function with a variable number of arguments:
import { MKMap } from "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.