javascript-collections-framework
    Preparing search index...

    javascript-collections-framework

    javascript-collections-framework

    CI coverage npm size dependencies

    JavaScript has no sorted map, no priority queue, and no way to use an object such as { x: 1, y: 2 } as a Map key compared by value; this library adds them, with deques, linked and LRU maps, and live views.

    Zero runtime dependencies · TypeScript types included · Runs on Node.js, Bun, Deno, Chrome, Firefox, Cloudflare Workers and React Native.

    import { HashMap, PriorityQueue, TreeMap } from 'javascript-collections-framework'

    const bookings = new TreeMap<number, string>().set(900, 'stand-up').set(1030, 'review')
    bookings.floorEntry(1100)?.value // 'review': the booking at or before 11:00

    new PriorityQueue([5, 1, 3]).poll() // 1: the smallest first

    type Point = { x: number; y: number }
    const places = new HashMap<Point, string>({ key: (p) => `${p.x},${p.y}` })
    places.set({ x: 1, y: 2 }, 'home').size // 1
    places.get({ x: 1, y: 2 }) // 'home': an equal point, not the stored object
    new Map([[{ x: 1, y: 2 }, 'home']]).get({ x: 1, y: 2 }) // undefined: a native Map compares by reference

    Try it in your browser, browse the recipes, see the benchmarks, or read coming from native JavaScript.

    Status: 0.x. Published on npm. Until 1.0.0, a minor release may contain breaking changes, each listed in the changelog; see versioning.

    npm install javascript-collections-framework
    pnpm add javascript-collections-framework
    yarn add javascript-collections-framework
    bun add javascript-collections-framework

    Or without a build step:

    <script src="https://cdn.jsdelivr.net/npm/javascript-collections-framework@0.2.0/dist/index.iife.js"
    integrity="sha384-NIHveBN5M28h7ISXDEpd3Je0gdtQul3REEShFOvzYtYaaZEhhgjTfSjDPypLbvxK" crossorigin="anonymous"></script>

    A map that keeps the most recently used entries, evicting the least recently used:

    import { LinkedHashMap } from 'javascript-collections-framework'

    const cache = new LinkedHashMap<string, number>({ accessOrder: true, maxSize: 2 })
    cache.set('a', 1).set('b', 2).size // 2
    cache.get('a') // 1: 'a' is now the most recently used
    cache.set('c', 3).has('b') // false: 'b', the least recently used, was evicted
    ;[...cache.keys()] // ['a', 'c']

    Every collection is iterable and converts to plain arrays and JSON:

    import { TreeSet } from 'javascript-collections-framework'

    const sorted = new TreeSet([3, 1, 2])
    ;[...sorted] // [1, 2, 3]
    Array.from(sorted) // [1, 2, 3]
    const json = JSON.stringify(sorted) // '[1,2,3]'
    TreeSet.from(JSON.parse(json)).equals(sorted) // true

    JavaScript ships Array, Map and Set. Programs that need a sorted map, a deque, a priority queue, a linked list, or a set keyed by value rather than by reference have to assemble them by hand. This library provides those structures behind a small set of shared interfaces, so that code written against an interface such as List, Queue or Deque works with any implementation.

    This library Native Array/Map/Set mnemonist js-sdsl @datastructures-js
    Shared interfaces, interchangeable implementations Yes: List, Queue, Deque, SortedMap and more No No Partly: common container base classes No: one package per structure
    Sorted maps and sets with range queries Yes: TreeMap, TreeSet, range views No No OrderedMap, OrderedSet with lower and upper bounds No
    Live views (sub-lists, ranges, reversed) Yes No No No No
    Value equality for keys Yes: key and equivalence options No No No No
    LRU map Yes: LinkedHashMap No Yes: LRUMap, LRUCache No No
    Runtime dependencies 0 — 1 0 0 or 1 (priority-queue depends on heap)
    TypeScript types Bundled Built in Bundled Bundled Bundled
    Sorted map, else priority queue, min + gzip 12.5 kB (TreeMap), 6.0 kB (PriorityQueue) 0 2.7 kB, Heap (no sorted map) 3.4 kB, OrderedMap 1.7 kB, PriorityQueue

    Checked on 2026-11-19 against mnemonist 0.40.8, js-sdsl 4.4.2, @datastructures-js/priority-queue 6.4.0, @datastructures-js/deque 1.0.8 and @datastructures-js/linked-list 6.1.4. Sizes are one export bundled alone, minified and gzipped, the way the size table measures this library. Importing every export at once: 39.5 kB (whole package).

    Bar chart of other libraries' median operations per second relative to javascript-collections-framework at 100,000 elements, on a log scale centred on 1×; another library's median is higher in 6 of 24 rows.

    When another choice is better:

    • Native Array, Map and Set when keys are primitives or compared by reference and no order, range or queue is needed: they are built into the engine and add nothing to a bundle.
    • mnemonist for its breadth: tries, BK-trees, bloom filters, sparse sets and other specialised structures this library does not have.
    • js-sdsl for the smallest sorted map: its OrderedMap is about a quarter of the size of TreeMap, with iterators in the style of the C++ standard library.
    • @datastructures-js when one structure is all that is needed: each is a small package of its own.
    Family Implementations
    Lists ArrayList
    Sets HashSet, LinkedHashSet, TreeSet
    Maps HashMap, LinkedHashMap, TreeMap
    Queues and deques ArrayDeque, LinkedList, PriorityQueue
    Sets from maps newSetFromMap
    Immutable listOf, setOf, mapOf, copyOfList, copyOfSet, copyOfMap
    Read-only views unmodifiableList, unmodifiableSet, unmodifiableMap and six more
    Lists over arrays asList
    Algorithms binarySearch, sortedIndex, rotate, shuffle, min, max and seven more
    • One model. Shared interfaces (Collection, List, Queue, Deque, sorted and navigable maps and sets) with interchangeable implementations, plus iterables, Symbol.iterator, for…of and spread throughout.
    • Typed. Written in TypeScript with full generic types; works from plain JavaScript.
    • Documented cost. Every operation states its time complexity.
    • Zero runtime dependencies. Tree-shakeable ESM, with a CommonJS build for older tooling.
    • Runs everywhere. ES2015 output with no platform APIs: Node.js, Bun, Deno, browsers, edge runtimes and React Native, from TypeScript or plain JavaScript, via ESM, CommonJS or a <script> tag. See compatibility for what is tested today.

    Every collection planned so far is available. New ones are listed here, and move to the documentation when they ship.

    Using collections with React, Vue, Svelte or other frameworks, or sending them through JSON or workers: see docs/frameworks.md.

    New to the library? The behaviour notes list what most often surprises newcomers.

    See docs/, and the API reference for every export. What each release may change is in versioning. What is tested, at what scale, and what is not, is in how this library is tested.

    Zero runtime dependencies and no install scripts. To report a vulnerability, see SECURITY.md.

    See CONTRIBUTING.md.

    MIT