Mnemonist Data Structures Library
Mnemonist is a comprehensive, curated collection of over 50 data structures for JavaScript and TypeScript. It includes classic structures like Heaps, Queues, and LRU Caches, alongside more specialized and exotic options such as Burkhard-Keller Trees, Bloom Filters, and Fibonacci Heaps. The library emphasizes high performance, modularity, ease of use with an API consistent with standard JavaScript objects, and full TypeScript support. The current stable version is 0.40.3, with minor updates and fixes released regularly, often multiple times a month, addressing bug fixes and type declaration improvements. Its key differentiators are the sheer breadth of its offerings, from foundational structures to highly specialized algorithms for information retrieval and metric space indexation, all while maintaining a focus on performance and robust typing. Mnemonist does not include a Graph data structure due to its typical complexity.
Common errors
-
TypeError: (0 , mnemonist_1.Set) is not a function
cause Attempting to use the `Set` operations module with the uppercase 'Set' path after v0.40.0, where it was renamed to lowercase 'set'.fixUpdate your import statement for Set operations from `import { operation } from 'mnemonist/Set';` to `import { operation } from 'mnemonist/set';` -
TypeError: __webpack_require__(...).Heap is not a constructor
cause This error, common in bundled environments, indicates an incorrect import or module resolution, often when using `require()` syntax in an ESM context or an incorrect named import path.fixEnsure you are using specific, correct import paths like `import { Heap } from 'mnemonist/heap';`. If using CommonJS, `const Heap = require('mnemonist/heap');`. Verify your bundler configuration for proper module resolution, especially if on versions prior to 0.40.0.
Warnings
- breaking The module for generic Set operations (e.g., `intersection`, `union`) was renamed from `Set` to `set` (lowercase) in v0.40.0. This was done to resolve a CommonJS named export collision. Users should update their import paths.
- gotcha Prior to v0.40.0, Mnemonist primarily supported CommonJS modules. While browser bundling was possible, direct ESM named exports were not officially supported. Version 0.40.0 introduced comprehensive ESM named exports, improving compatibility with modern build tools and environments.
- gotcha Several type declaration fixes for specific data structures (e.g., `BloomFilter.from`, `SparseMap` constructor overloads, `BiMap.get`) were released in recent minor versions. Users relying heavily on TypeScript might encounter type-related issues or missing overloads if they are not on the latest minor release for their major version.
Install
-
npm install mnemonist -
yarn add mnemonist -
pnpm add mnemonist
Imports
- Heap
const Heap = require('mnemonist/heap');import { Heap } from 'mnemonist/heap'; - BloomFilter
import { BloomFilter } from 'mnemonist';import { BloomFilter } from 'mnemonist/bloom-filter'; - set
import { intersection } from 'mnemonist/Set';import { intersection } from 'mnemonist/set';
Quickstart
import { Heap } from 'mnemonist/heap';
import { BloomFilter } from 'mnemonist/bloom-filter';
import { BiMap } from 'mnemonist/bi-map';
import { intersection } from 'mnemonist/set'; // Note: 'set' module name for operations since v0.40.0
console.log('--- Using a Min-Heap ---');
const minHeap = new Heap<number>((a, b) => a - b); // Custom comparator for min-heap
minHeap.push(5);
minHeap.push(2);
minHeap.push(8);
console.log('Heap size:', minHeap.size); // Expected: 3
console.log('Min element:', minHeap.pop()); // Expected: 2
console.log('Min element after pop:', minHeap.pop()); // Expected: 5
console.log('\n--- Using a Bloom Filter ---');
const filter = new BloomFilter(100, 3); // 100 bits, 3 hash functions
filter.add('apple');
filter.add('banana');
console.log('Contains "apple":', filter.has('apple')); // Expected: true
console.log('Contains "orange":', filter.has('orange')); // Expected: false (or very likely false)
console.log('\n--- Using a BiMap ---');
const biMap = new BiMap<string, number>();
biMap.set('one', 1);
biMap.set('two', 2);
console.log('Value for "one":', biMap.get('one')); // Expected: 1
console.log('Key for 2 via inverse map:', biMap.inverse.get(2)); // Expected: "two"
console.log('Has "three":', biMap.has('three')); // Expected: false
console.log('\n--- Using Set Operations ---');
const setA = new Set([1, 2, 3]);
const setB = new Set([2, 3, 4]);
const commonElements = intersection(setA, setB);
console.log('Intersection of {1,2,3} and {2,3,4}:', Array.from(commonElements)); // Expected: [2, 3]