Mnemonist Data Structures Library

0.40.3 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the instantiation and basic usage of a Min-Heap, a Bloom Filter, a BiMap, and a set intersection operation, showcasing common patterns for importing and interacting with Mnemonist data structures.

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]

view raw JSON →