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

linear.mddocs/

Linear Data Structures

Classic linear data structures including queues, stacks, linked lists, and circular buffers.

Capabilities

Queue

FIFO (First In, First Out) queue implementation.

/**
 * FIFO queue for ordered processing
 */
function Queue<T>(): Queue<T>;

interface Queue<T> {
  readonly size: number;
  
  /** Add item to back of queue */
  enqueue(item: T): number;
  
  /** Remove and return item from front of queue */
  dequeue(): T | undefined;
  
  /** View item at front without removing */
  peek(): T | undefined;
  
  /** Remove all items */
  clear(): void;
  
  values(): IterableIterator<T>;
  [Symbol.iterator](): IterableIterator<T>;
  forEach(callback: (item: T, index: number, queue: this) => void): void;
  
  inspect(): any;
}

Stack

LIFO (Last In, First Out) stack implementation.

/**
 * LIFO stack for recursive operations
 */
function Stack<T>(): Stack<T>;

interface Stack<T> {
  readonly size: number;
  
  /** Add item to top of stack */
  push(item: T): number;
  
  /** Remove and return item from top of stack */
  pop(): T | undefined;
  
  /** View item at top without removing */
  peek(): T | undefined;
  
  /** Remove all items */
  clear(): void;
  
  values(): IterableIterator<T>;
  [Symbol.iterator](): IterableIterator<T>;
  forEach(callback: (item: T, index: number, stack: this) => void): void;
  
  inspect(): any;
}

FixedStack

Fixed-capacity stack with typed array backing storage.

/**
 * Fixed-capacity stack with typed backing storage
 * @param ArrayClass Constructor for backing array type
 * @param capacity Maximum number of items
 */
function FixedStack<T>(ArrayClass: ArrayConstructor, capacity: number): FixedStack<T>;

interface FixedStack<T> {
  readonly capacity: number;
  readonly size: number;
  
  /** Add item to top (throws if at capacity) */
  push(item: T): number;
  
  /** Remove and return item from top */
  pop(): T | undefined;
  
  /** View item at top without removing */
  peek(): T | undefined;
  
  /** Remove all items */
  clear(): void;
  
  values(): IterableIterator<T>;
  [Symbol.iterator](): IterableIterator<T>;
  
  inspect(): any;
}

LinkedList

Doubly-linked list with efficient insertion and deletion.

/**
 * Doubly-linked list implementation
 */
function LinkedList<T>(): LinkedList<T>;

interface LinkedList<T> {
  readonly size: number;
  
  /** Add item to end of list */
  append(value: T): number;
  
  /** Add item to beginning of list */
  prepend(value: T): number;
  
  /** Remove and return first item */
  shift(): T | undefined;
  
  /** Remove and return last item */
  pop(): T | undefined;
  
  /** Insert item at specific index */
  insert(index: number, value: T): number;
  
  /** Remove item at specific index */
  remove(index: number): T | undefined;
  
  /** Get item at index without removing */
  get(index: number): T | undefined;
  
  /** Set item at index */
  set(index: number, value: T): void;
  
  /** Find index of first occurrence of value */
  indexOf(value: T): number;
  
  /** Find index of last occurrence of value */
  lastIndexOf(value: T): number;
  
  /** Check if value exists in list */
  contains(value: T): boolean;
  
  /** Convert to array */
  toArray(): Array<T>;
  
  /** Remove all items */
  clear(): void;
  
  values(): IterableIterator<T>;
  [Symbol.iterator](): IterableIterator<T>;
  forEach(callback: (item: T, index: number, list: this) => void): void;
  
  inspect(): any;
}

CircularBuffer

Fixed-size circular buffer with efficient wrap-around operations.

/**
 * Fixed-size circular buffer
 * @param ArrayClass Constructor for backing array type
 * @param capacity Fixed buffer size
 */
function CircularBuffer<T>(ArrayClass: ArrayConstructor, capacity: number): CircularBuffer<T>;

interface CircularBuffer<T> {
  readonly capacity: number;
  readonly size: number;
  
  /** Add item (overwrites oldest if full) */
  push(item: T): number;
  
  /** Remove and return oldest item */
  shift(): T | undefined;
  
  /** Get item at logical index */
  get(index: number): T | undefined;
  
  /** Set item at logical index */
  set(index: number, value: T): void;
  
  /** Remove all items */
  clear(): void;
  
  values(): IterableIterator<T>;
  [Symbol.iterator](): IterableIterator<T>;
  
  inspect(): any;
}

FixedDeque

Fixed-capacity double-ended queue (deque) with efficient operations at both ends.

/**
 * Fixed-capacity double-ended queue
 * @param ArrayClass Constructor for backing array type
 * @param capacity Maximum number of items
 */
function FixedDeque<T>(ArrayClass: ArrayConstructor, capacity: number): FixedDeque<T>;

interface FixedDeque<T> {
  readonly capacity: number;
  readonly size: number;
  
  /** Add item to back */
  push(item: T): number;
  
  /** Add item to front */
  unshift(item: T): number;
  
  /** Remove and return item from back */
  pop(): T | undefined;
  
  /** Remove and return item from front */
  shift(): T | undefined;
  
  /** View item at back without removing */
  peekLast(): T | undefined;
  
  /** View item at front without removing */
  peekFirst(): T | undefined;
  
  /** Remove all items */
  clear(): void;
  
  values(): IterableIterator<T>;
  [Symbol.iterator](): IterableIterator<T>;
  
  inspect(): any;
}

Usage Examples

Queue Example

import {Queue} from "mnemonist";

// Task processing queue
const taskQueue = new Queue<Task>();

// Add tasks
taskQueue.enqueue({id: 1, name: "Process order"});
taskQueue.enqueue({id: 2, name: "Send email"});
taskQueue.enqueue({id: 3, name: "Update database"});

console.log(taskQueue.size); // 3

// Process tasks in order
while (taskQueue.size > 0) {
  const task = taskQueue.dequeue();
  console.log(`Processing: ${task?.name}`);
}

// Peek without removing
taskQueue.enqueue({id: 4, name: "Cleanup"});
console.log(taskQueue.peek()?.name); // "Cleanup"
console.log(taskQueue.size); // Still 1

Stack Example

import {Stack} from "mnemonist";

// Expression evaluation stack
const operatorStack = new Stack<string>();

// Push operators
operatorStack.push("(");
operatorStack.push("+");
operatorStack.push("*");

console.log(operatorStack.peek()); // "*"
console.log(operatorStack.pop());  // "*"
console.log(operatorStack.size);   // 2

// Iterate from top to bottom
for (const op of operatorStack) {
  console.log(op); // "+", "("
}

LinkedList Example

import {LinkedList} from "mnemonist";

const list = new LinkedList<number>();

// Add items
list.append(1);
list.append(2);
list.prepend(0); // [0, 1, 2]

// Insert at specific position
list.insert(1, 0.5); // [0, 0.5, 1, 2]

// Access items
console.log(list.get(0)); // 0
console.log(list.indexOf(1)); // 2

// Remove items
list.remove(1); // Remove at index 1
list.pop();     // Remove last
list.shift();   // Remove first

// Convert to array
const array = list.toArray();

CircularBuffer Example

import {CircularBuffer} from "mnemonist";

// Sliding window buffer
const window = new CircularBuffer(Array, 5); // Capacity of 5

// Fill buffer
for (let i = 1; i <= 7; i++) {
  window.push(i);
}

console.log(window.size); // 5 (capacity reached)

// Buffer contains [3, 4, 5, 6, 7] (oldest items overwritten)
for (const value of window) {
  console.log(value); // 3, 4, 5, 6, 7
}

// Access by index
console.log(window.get(0)); // 3 (oldest)
console.log(window.get(4)); // 7 (newest)

FixedDeque Example

import {FixedDeque} from "mnemonist";

// Job scheduler with priority
const scheduler = new FixedDeque(Array, 100);

// Add high priority job to front
scheduler.unshift({priority: "high", task: "urgent"});

// Add normal priority job to back
scheduler.push({priority: "normal", task: "routine"});

// Process high priority first
const urgent = scheduler.shift(); // Gets high priority job
const routine = scheduler.pop();  // Gets normal priority job

// Peek without removing
scheduler.push({priority: "low", task: "cleanup"});
console.log(scheduler.peekLast()); // Latest added job
console.log(scheduler.peekFirst()); // Next job to process

Performance Characteristics

OperationQueueStackLinkedListCircularBufferFixedDeque
Push/EnqueueO(1)O(1)O(1)O(1)O(1)
Pop/DequeueO(1)O(1)O(1)O(1)O(1)
PeekO(1)O(1)O(1)O(1)O(1)
InsertN/AN/AO(n)N/AN/A
RemoveN/AN/AO(n)N/AN/A
Random AccessN/AN/AO(n)O(1)N/A
SpaceO(n)O(n)O(n)O(capacity)O(capacity)

Memory Characteristics

  • Queue/Stack: Dynamic sizing, no memory limit
  • FixedStack: Uses typed arrays, fixed capacity
  • LinkedList: Node-based, efficient insertion/deletion anywhere
  • CircularBuffer: Fixed memory footprint, overwrites old data
  • FixedDeque: Fixed capacity with efficient operations at both ends

Choosing the Right Linear Structure

  • Queue: For FIFO processing, task queues, breadth-first algorithms
  • Stack: For LIFO processing, recursion simulation, expression parsing
  • FixedStack: When memory usage must be bounded and you need typed storage
  • LinkedList: When you need frequent insertion/deletion at arbitrary positions
  • CircularBuffer: For sliding windows, streaming data with fixed memory
  • FixedDeque: When you need efficient operations at both ends with bounded memory

docs

caches.md

heaps.md

index.md

linear.md

maps.md

sets.md

specialized.md

trees.md

vectors.md

tile.json