JavaScript Standard Data Structures Library

4.4.2 · active · verified Sun Apr 19

js-sdsl is a comprehensive JavaScript library providing various standard data structures, designed to offer high performance comparable to C++ STL. It includes implementations for structures like Stack, Queue, PriorityQueue, Vector, LinkedList, Deque, OrderedSet, OrderedMap, HashSet, and HashMap. The library is currently on stable version 4.4.2, actively maintained with a regular release cadence as seen by frequent updates within the 4.x series. Its key differentiators include optimized performance that often surpasses other popular JavaScript data structure libraries (e.g., Denque), a lightweight footprint (~9KB compressed), and a lack of external dependencies. It also provides C++ STL-like bidirectional iterators and ships with full TypeScript type definitions, making it suitable for modern JavaScript and TypeScript projects that require efficient, robust data management.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the instantiation and basic operations of OrderedMap, Deque, and Vector, including adding, retrieving, iterating, and modifying elements.

import { OrderedMap, Deque, Vector } from 'js-sdsl';

// Using an OrderedMap (a sorted map implemented with a red-black tree)
const myOrderedMap = new OrderedMap<string, number>();
myOrderedMap.set('apple', 10);
myOrderedMap.set('banana', 20);
myOrderedMap.set('cherry', 5);
console.log('OrderedMap size:', myOrderedMap.size()); // Output: 3
console.log('Value of apple:', myOrderedMap.get('apple')); // Output: 10
myOrderedMap.forEach((key, value) => {
  console.log(`Map: ${key} -> ${value}`);
});
// Output:
// Map: apple -> 10
// Map: banana -> 20
// Map: cherry -> 5

// Using a Deque (Double-ended queue)
const myDeque = new Deque<string>();
myDeque.pushFront('first');
myDeque.pushBack('second');
myDeque.pushFront('zero');
console.log('Deque elements:', myDeque.toArray()); // Output: [ 'zero', 'first', 'second' ]
console.log('Pop back:', myDeque.popBack()); // Output: second
console.log('Pop front:', myDeque.popFront()); // Output: zero

// Using a Vector (a protected array)
const myVector = new Vector<number>([1, 2, 3]);
myVector.pushBack(4);
myVector.insert(0, 0);
console.log('Vector elements:', myVector.toArray()); // Output: [ 0, 1, 2, 3, 4 ]
console.log('Element at index 2:', myVector.getElementByPos(2)); // Output: 2

view raw JSON →