{"id":11155,"library":"js-sdsl","title":"JavaScript Standard Data Structures Library","description":"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.","status":"active","version":"4.4.2","language":"javascript","source_language":"en","source_url":"https://github.com/js-sdsl/js-sdsl","tags":["javascript","data","structure","data structure","rbTree","rbtree","RBTree","red black tree","ordered","typescript"],"install":[{"cmd":"npm install js-sdsl","lang":"bash","label":"npm"},{"cmd":"yarn add js-sdsl","lang":"bash","label":"yarn"},{"cmd":"pnpm add js-sdsl","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM imports are the recommended standard. For v4+, direct CommonJS `require('js-sdsl').OrderedMap` may not work as expected due to changes in CJS target to ES6 in v4.1.0. Use `import()` for dynamic CJS loading if strictly necessary, but prefer ESM.","wrong":"const OrderedMap = require('js-sdsl').OrderedMap;","symbol":"OrderedMap","correct":"import { OrderedMap } from 'js-sdsl';"},{"note":"As with all exports from js-sdsl v4+, prefer named ESM imports. This ensures correct tree-shaking and module resolution in modern build environments.","wrong":"const Vector = require('js-sdsl').Vector;","symbol":"Vector","correct":"import { Vector } from 'js-sdsl';"},{"note":"In version 4.x and later, the `Set` and `Map` classes were renamed to `OrderedSet` and `OrderedMap` respectively to avoid conflicts with native JavaScript `Set` and `Map` global objects.","wrong":"import { Set } from 'js-sdsl'; // Incorrect symbol name for v4+","symbol":"OrderedSet","correct":"import { OrderedSet } from 'js-sdsl';"},{"note":"Always explicitly import the specific data structure classes you need. Direct `require('js-sdsl')` will not expose the individual classes in a usable way for CJS without further destructuring. ESM named imports are cleaner.","wrong":"const Deque = require('js-sdsl'); // CommonJS import often requires destructuring for named exports","symbol":"Deque","correct":"import { Deque } from 'js-sdsl';"}],"quickstart":{"code":"import { OrderedMap, Deque, Vector } from 'js-sdsl';\n\n// Using an OrderedMap (a sorted map implemented with a red-black tree)\nconst myOrderedMap = new OrderedMap<string, number>();\nmyOrderedMap.set('apple', 10);\nmyOrderedMap.set('banana', 20);\nmyOrderedMap.set('cherry', 5);\nconsole.log('OrderedMap size:', myOrderedMap.size()); // Output: 3\nconsole.log('Value of apple:', myOrderedMap.get('apple')); // Output: 10\nmyOrderedMap.forEach((key, value) => {\n  console.log(`Map: ${key} -> ${value}`);\n});\n// Output:\n// Map: apple -> 10\n// Map: banana -> 20\n// Map: cherry -> 5\n\n// Using a Deque (Double-ended queue)\nconst myDeque = new Deque<string>();\nmyDeque.pushFront('first');\nmyDeque.pushBack('second');\nmyDeque.pushFront('zero');\nconsole.log('Deque elements:', myDeque.toArray()); // Output: [ 'zero', 'first', 'second' ]\nconsole.log('Pop back:', myDeque.popBack()); // Output: second\nconsole.log('Pop front:', myDeque.popFront()); // Output: zero\n\n// Using a Vector (a protected array)\nconst myVector = new Vector<number>([1, 2, 3]);\nmyVector.pushBack(4);\nmyVector.insert(0, 0);\nconsole.log('Vector elements:', myVector.toArray()); // Output: [ 0, 1, 2, 3, 4 ]\nconsole.log('Element at index 2:', myVector.getElementByPos(2)); // Output: 2\n","lang":"typescript","description":"This quickstart demonstrates the instantiation and basic operations of OrderedMap, Deque, and Vector, including adding, retrieving, iterating, and modifying elements."},"warnings":[{"fix":"Rename `Set` to `OrderedSet` and `Map` to `OrderedMap` in your code. Update method calls from `eraseElementByValue` to `eraseElementByKey` for affected containers.","message":"In version 4.x, the classes `Set` and `Map` were renamed to `OrderedSet` and `OrderedMap` respectively. If you are migrating from v3 or earlier, you must update your imports and class instantiations to use the new names to avoid conflicts with native JavaScript `Set` and `Map` objects. Additionally, `eraseElementByValue` on `HashSet` and `OrderedSet` was renamed to `eraseElementByKey` in v4.0.0.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Benchmark critical sections of your application to determine if `js-sdsl` provides a meaningful performance improvement for your specific use case, especially for smaller datasets.","message":"While `js-sdsl` offers high-performance data structures, for very small datasets or simple array/object operations, native JavaScript `Array` or `Map`/`Set` might sometimes exhibit comparable or even better performance due to V8 engine optimizations. `js-sdsl` shines in scenarios requiring specific algorithmic complexities, ordered iteration, or large-scale data manipulation where its optimized implementations provide consistent benefits.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Refactor your module imports to use ESM `import { Name } from 'js-sdsl';` syntax. If you are in a pure CommonJS environment, consider using dynamic `import('js-sdsl').then(...)` or ensure your build setup correctly transpiles ESM to CJS with named exports.","message":"With version 4.1.0, the CommonJS target was changed to ES6. While `js-sdsl` generally aims for broad compatibility, modern Node.js environments and bundlers primarily favor ES Modules (ESM). Using `require()` for named exports in CJS contexts with `js-sdsl` v4+ may lead to unexpected behavior or `undefined` imports.","severity":"breaking","affected_versions":">=4.1.0"},{"fix":"If experiencing unexpected iterator behavior, review the `CHANGELOG.md` for your specific `js-sdsl` version regarding iterator updates and adjust custom iterator logic or assumptions accordingly.","message":"Iterator behavior and internal implementations were subject to changes in the 4.x series. Specifically, `OrderedMap`'s iterator pointer retrieval changed from `Object.defineProperty` to `Proxy` in v4.1.4, and iterator type descriptions evolved. While generally backward-compatible for common use, advanced iterator manipulation might require re-testing.","severity":"gotcha","affected_versions":">=4.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure you are using `import { OrderedMap } from 'js-sdsl';` in ESM or `const { OrderedMap } = require('js-sdsl');` in CJS. For v4+, ESM is highly recommended.","cause":"Attempting to use `OrderedMap` (or other classes) without a proper ESM named import, or an incorrect CommonJS `require` call that does not destructure the named export.","error":"ReferenceError: OrderedMap is not defined"},{"fix":"Verify that `container` is correctly initialized, for example: `const container = new OrderedMap<KeyType, ValueType>();`. Also, ensure you are calling the correct method for the specific data structure (e.g., `OrderedSet` uses `add` instead of `set`).","cause":"This error typically occurs when attempting to call a method like `set` on a variable that is either not an instance of the expected data structure (e.g., `OrderedMap`), or has not been properly initialized.","error":"TypeError: container.set is not a function"},{"fix":"Always check iterator validity before dereferencing or using it. Ensure that modifications to the container do not invalidate active iterators that are subsequently used. The library implements C++ STL-like iterators, so similar rules regarding iterator invalidation apply.","cause":"This error message indicates an invalid operation on an iterator, possibly due to accessing an iterator that has become invalid (e.g., after an element it pointed to was removed) or attempting to dereference an 'end' iterator.","error":"Error: Iterator access denied"}],"ecosystem":"npm"}