CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-mnemonist

Curated collection of efficient data structures for JavaScript/TypeScript applications.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Mnemonist

Mnemonist 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.

Package Information

  • Package Name: mnemonist
  • Package Type: npm
  • Language: JavaScript with TypeScript definitions
  • Installation: npm install mnemonist
  • Repository: https://github.com/yomguithereal/mnemonist
  • Version: 0.40.3

Core Imports

ES Modules (Recommended)

// 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";

CommonJS

// 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");

Full Library Import (Not Recommended for Production)

import * as mnemonist from "mnemonist";
const heap = new mnemonist.Heap();

Basic Usage

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")); // true

Architecture

Mnemonist is designed with several key principles:

  • Memory Efficiency: Optimized implementations using typed arrays and minimal object allocations
  • Performance: O(1), O(log n), and other optimal time complexities for core operations
  • Type Safety: Complete TypeScript definitions with generic type parameters
  • Modularity: Individual imports available for optimal bundle size
  • Consistency: Common method patterns across all data structures

Common Interface Patterns

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;
}

Capabilities

Heaps & Priority Queues

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>;

Heaps & Priority Queues

Vectors & Arrays

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;

Vectors & Arrays

Caches

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>;

Caches

Maps & Associative Collections

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

Sets & Set Operations

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>;
}

Sets & Set Operations

Trees & Hierarchical Structures

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

Linear Data 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>;

Linear Data Structures

Specialized Algorithms & Structures

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

Type Definitions

Common Types

// 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;

Iterator Types

// Standard iteration interfaces implemented by most structures
interface Iterator<T> {
  next(): IteratorResult<T>;
  [Symbol.iterator](): Iterator<T>;
}

interface IteratorResult<T> {
  done: boolean;
  value: T;
}

Performance Characteristics

Mnemonist data structures are optimized for performance with the following general characteristics:

  • Heap operations: O(log n) insertion/deletion, O(1) peek
  • Cache operations: O(1) get/set/delete with LRU eviction
  • Vector operations: O(1) amortized append, O(1) random access
  • Trie operations: O(k) where k is key length
  • Spatial tree queries: O(log n) average case for nearest neighbor
  • Set operations: O(1) for sparse integer sets, O(n) for functional operations

Dependencies

  • obliterator (^2.0.4): Iterator utilities and functional programming helpers used internally by some data structures

The library is designed to minimize external dependencies while providing comprehensive functionality.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/mnemonist@0.40.x
Publish Source
CLI
Badge
tessl/npm-mnemonist badge