Curated collection of efficient data structures for JavaScript/TypeScript applications.
npx @tessl/cli install tessl/npm-mnemonist@0.40.0Mnemonist is a comprehensive JavaScript data structures library providing efficient implementations of 40+ data structures for JavaScript and TypeScript applications. It features classic structures like heaps, tries, linked lists, LRU caches, alongside specialized implementations including Fibonacci heaps, K-D trees, bloom filters, bit vectors, sparse sets, and metric space indexation structures.
npm install mnemonist// Import specific data structures
import {Heap, LRUCache, BitSet, Trie} from "mnemonist";
// Import with types
import {Heap, MinHeap, MaxHeap} from "mnemonist";
// Import utility functions
import {nsmallest, nlargest} from "mnemonist";
// Import individual modules
import Heap from "mnemonist/heap";
import LRUCache from "mnemonist/lru-cache";// Import specific data structures
const {Heap, LRUCache, BitSet, Trie} = require("mnemonist");
// Import utility functions
const {nsmallest, nlargest} = require("mnemonist");
// Import individual modules
const Heap = require("mnemonist/heap");
const LRUCache = require("mnemonist/lru-cache");import * as mnemonist from "mnemonist";
const heap = new mnemonist.Heap();import {Heap, LRUCache, BitSet, Trie} from "mnemonist";
// Priority queue with heap
const minHeap = new Heap();
minHeap.push(5, 2, 8, 1);
console.log(minHeap.pop()); // 1
// LRU Cache
const cache = new LRUCache(100);
cache.set("key", "value");
console.log(cache.get("key")); // "value"
// Bit operations
const bitSet = new BitSet(32);
bitSet.set(5, 1);
console.log(bitSet.test(5)); // true
// Prefix tree
const trie = new Trie();
trie.add("hello");
trie.add("help");
console.log(trie.has("help")); // trueMnemonist is designed with several key principles:
Most data structures in mnemonist follow consistent patterns:
interface CommonMethods<T> {
// Core operations
clear(): void;
size: number;
// Iteration support
values(): Iterator<T>;
entries(): Iterator<[any, T]>;
keys(): Iterator<any>;
forEach(callback: (value: T, key?: any) => void): void;
[Symbol.iterator](): Iterator<T>;
// Debugging
inspect(): any;
}High-performance priority queues for efficient minimum/maximum element access with logarithmic insertion and removal.
// Basic heap operations
function Heap<T>(comparator?: (a: T, b: T) => number): Heap<T>;
function MinHeap<T>(comparator?: (a: T, b: T) => number): MinHeap<T>;
function MaxHeap<T>(comparator?: (a: T, b: T) => number): MaxHeap<T>;
function FibonacciHeap<T>(comparator?: (a: T, b: T) => number): FibonacciHeap<T>;Memory-efficient resizable arrays with typed backing storage and bit-packed boolean arrays for space optimization.
// Vector constructors
function Vector<T>(ArrayClass: ArrayConstructor, capacity?: number): Vector<T>;
function BitVector(length: number): BitVector;
function BitSet(length: number): BitSet;
// Typed vector variants
function Uint8Vector(capacity?: number): Uint8Vector;
function Float32Vector(capacity?: number): Float32Vector;
function PointerVector(capacity?: number): PointerVector;LRU (Least Recently Used) caches with configurable capacity and efficient eviction policies for memory management.
// Cache constructors
function LRUCache<K, V>(capacity: number): LRUCache<K, V>;
function LRUCacheWithDelete<K, V>(capacity: number): LRUCacheWithDelete<K, V>;
function LRUMap<K, V>(capacity: number): LRUMap<K, V>;Advanced mapping structures including bidirectional maps, multi-value maps, and fuzzy matching capabilities.
// Map constructors
function BiMap<K, V>(): BiMap<K, V>;
function MultiMap<K, V>(container?: ArrayConstructor | SetConstructor): MultiMap<K, V>;
function DefaultMap<K, V>(factory: () => V): DefaultMap<K, V>;
function FuzzyMap<V>(distance: DistanceFunction, tolerance: number): FuzzyMap<V>;Maps & Associative Collections
Efficient set implementations including sparse sets for integer keys and functional set operations.
// Set constructors
function SparseSet(capacity: number): SparseSet;
function SparseQueueSet(capacity: number): SparseQueueSet;
function StaticDisjointSet(size: number): StaticDisjointSet;
// Set operations namespace
namespace set {
function intersection<T>(a: Iterable<T>, b: Iterable<T>): Set<T>;
function union<T>(a: Iterable<T>, b: Iterable<T>): Set<T>;
function difference<T>(a: Iterable<T>, b: Iterable<T>): Set<T>;
}Tree data structures for prefix matching, spatial queries, fuzzy search, and range operations.
// Tree constructors
function Trie<T>(Token?: new () => T): Trie<T>;
function TrieMap<T, V>(Token?: new () => T): TrieMap<T, V>;
function BKTree(distance: DistanceFunction): BKTree;
function KDTree(dimensions: number, distance?: DistanceFunction): KDTree;
function VPTree(distance: DistanceFunction, items?: any[]): VPTree;Trees & Hierarchical Structures
Classic linear data structures including queues, stacks, linked lists, and circular buffers.
// Linear structure constructors
function Queue<T>(): Queue<T>;
function Stack<T>(): Stack<T>;
function LinkedList<T>(): LinkedList<T>;
function CircularBuffer<T>(ArrayClass: ArrayConstructor, capacity: number): CircularBuffer<T>;
function FixedDeque<T>(ArrayClass: ArrayConstructor, capacity: number): FixedDeque<T>;Advanced data structures for specific algorithmic applications including bloom filters, suffix arrays, and search indexes.
// Specialized structure constructors
function BloomFilter(capacity: number, errorRate?: number): BloomFilter;
function SuffixArray(string: string | string[]): SuffixArray;
function InvertedIndex(tokenizer?: TokenizerFunction): InvertedIndex;
function SymSpell(options?: SymSpellOptions): SymSpell;
function PassjoinIndex(options: PassjoinOptions): PassjoinIndex;Specialized Algorithms & Structures
// Comparison function used by heaps and sorted structures
type ComparatorFunction<T> = (a: T, b: T) => number;
// Distance function used by metric trees and fuzzy structures
type DistanceFunction<T = string> = (a: T, b: T) => number;
// Tokenizer function for text processing
type TokenizerFunction = (text: string) => string[];
// Array constructor types for typed backing storage
type ArrayConstructor =
| Uint8ArrayConstructor
| Uint16ArrayConstructor
| Uint32ArrayConstructor
| Int8ArrayConstructor
| Int16ArrayConstructor
| Int32ArrayConstructor
| Float32ArrayConstructor
| Float64ArrayConstructor;// Standard iteration interfaces implemented by most structures
interface Iterator<T> {
next(): IteratorResult<T>;
[Symbol.iterator](): Iterator<T>;
}
interface IteratorResult<T> {
done: boolean;
value: T;
}Mnemonist data structures are optimized for performance with the following general characteristics:
The library is designed to minimize external dependencies while providing comprehensive functionality.