Registry / database / tinyqueue

tinyqueue

JSON →
library3.0.0jsnpmunverified

tinyqueue is a lightweight, efficient JavaScript library providing a binary heap-based priority queue data structure. Currently at version 3.0.0, it offers fundamental operations like `push`, `pop`, and `peek` with minimal overhead. The library's core design prioritizes simplicity and a small bundle size, distinguishing it from more feature-rich or specialized queue implementations. It supports custom comparison functions, allowing users to define priority based on object properties rather than just raw values. While release cadence is not strictly regular, it sees updates for performance improvements and compatibility, with the most recent major update (v3.0.0) shifting to an ES module-only distribution.

database
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Since v3.0.0, tinyqueue is published exclusively as an ES module. CommonJS `require()` is no longer supported and will result in an error in modern Node.js environments. TypeScript types are bundled and automatically available for this default export.

import TinyQueue from 'tinyqueue';

Demonstrates how to initialize a TinyQueue, add elements, retrieve the highest priority item, inspect the queue's length, and use custom comparators for complex objects, including how to turn a queue into a sorted array.

import TinyQueue from 'tinyqueue'; // Create an empty priority queue const queue = new TinyQueue(); // Add some items queue.push(7); queue.push(5); queue.push(10); console.log('Initial top item:', queue.peek()); // Expected: 5 // Remove the top item const top = queue.pop(); // Returns 5 console.log('Popped item:', top); console.log('Current top item:', queue.peek()); // Expected: 7 console.log('Queue length:', queue.length); // Expected: 2 // Create a priority queue from an existing array const initialArrayQueue = new TinyQueue([7, 5, 10]); console.log('Queue from array, top:', initialArrayQueue.peek()); // Expected: 5 // Pass a custom item comparator for objects const customQueue = new TinyQueue([{value: 5}, {value: 7}, {value: 3}], function (a, b) { return a.value - b.value; }); console.log('Custom queue top item value:', customQueue.peek().value); // Expected: 3 // Turn a queue into a sorted array by repeatedly popping const sortedArray = []; while (customQueue.length) { sortedArray.push(customQueue.pop().value); } console.log('Sorted array from queue:', sortedArray); // Expected: [3, 5, 7]
Debug
Known footguns
breakingVersion 3.0.0 removed CommonJS and UMD distribution support. The package is now exclusively published as an ES module.
gotchaFor use cases strictly involving numerical priority queues where extreme performance is critical, the `flatqueue` library (from the same author) offers a potentially faster, specialized alternative.
gotchaWhen initializing a `TinyQueue` from an existing array, the input array is mutated (rearranged internally) by the queue's constructor to create the heap. This means the original array reference will point to a modified array.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
0 hits · last 30 days

No traffic data recorded yet.

Resources