Registry / data / flatqueue

flatqueue

JSON →
library3.0.0jsnpmunverified

flatqueue is a highly optimized JavaScript priority queue implementation that leverages a binary heap structure, distinguished by storing items and their numeric priorities in two separate, flat arrays. This design choice, while limiting custom comparator functions, enables significant performance gains, often several times faster than alternatives like tinyqueue. The package is currently at version 3.0.0, primarily focused on modern JavaScript environments, being ESM-only since version 2.0.0, with further streamlining in v3.0.0 by dropping the legacy UMD bundle. It ships with first-class TypeScript types via JSDoc. Its core differentiators include its minimalistic API, small footprint, and exceptional speed, particularly effective in scenarios requiring frequent push and pop operations, making it suitable for algorithms like A* pathfinding or event schedulers where performance is critical. Its release cadence indicates active maintenance, with recent versions focusing on performance optimizations and module system compatibility, ensuring it remains a performant choice for priority queue needs in web and Node.js applications.

npm install flatqueue
INSTALL
IMPORT
SIG · FLATQUEUE
F
flatqueue
datajavascriptv3.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

FlatQueue
import FlatQueue from 'flatqueue';
const FlatQueue = require('flatqueue');
FlatQueue is ESM-only since v2.0.0. UMD bundles were dropped in v3.0.0.
FlatQueue (CDN)
import FlatQueue from 'https://cdn.jsdelivr.net/npm/flatqueue/+esm';
For direct browser usage as an ES module via CDN.
TypeScript Types
import FlatQueue from 'flatqueue'; // Types are inferred via JSDoc
The package ships with first-class TypeScript types, automatically picked up by TypeScript compilers without explicit type imports.

This example demonstrates how to create a FlatQueue, push items with associated priorities, peek at the top item, pop the lowest-priority item, and clear the queue. It highlights basic API usage and the queue's length property.

import FlatQueue from 'flatqueue'; interface Item { id: number; priority: number; data: string; } const q = new FlatQueue(); const items: Item[] = [ { id: 0, priority: 5, data: 'task A' }, { id: 1, priority: 1, data: 'task B' }, { id: 2, priority: 8, data: 'task C' }, { id: 3, priority: 2, data: 'task D' } ]; console.log('Pushing items...'); for (let i = 0; i < items.length; i++) { // Push the item's original ID (or the item itself) and its priority value. // Storing only integers (like IDs/indices) is generally faster for JavaScript engine optimizations. q.push(items[i].id, items[i].priority); } console.log(`\nQueue length: ${q.length}`); console.log(`Top item (ID): ${q.peek()}`); // Expected: 1 (ID of task B) console.log(`Top item priority: ${q.peekValue()}`); // Expected: 1 const poppedId = q.pop(); // Removes ID 1 console.log(`Popped item ID: ${poppedId}`); // Expected: 1 console.log(`\nNew top item (ID): ${q.peek()}`); // Expected: 3 (ID of task D) console.log(`New top item priority: ${q.peekValue()}`); // Expected: 2 q.clear(); console.log(`\nQueue length after clear: ${q.length}`); // Expected: 0
Debug
Known issues
breakingFlatQueue transitioned to ESM-only in v2.0.0, dropping the CommonJS entry point. In v3.0.0, the legacy UMD bundle was also removed. Direct CommonJS `require()` statements will fail.
fix
Migrate your project to use ES module imports (`import FlatQueue from 'flatqueue';`) and ensure your environment (Node.js, bundler) is configured for ESM (e.g., `"type": "module"` in package.json).
affects: >=2.0.0
gotchaThe `pop()` and `clear()` methods do not automatically shrink the internal arrays (`ids` and `values`) to free memory. This behavior is intentional to improve performance by avoiding unnecessary reallocations during intensive reuse, but can lead to higher memory consumption if the queue size frequently fluctuates downwards and isn't explicitly managed.
fix
To explicitly release unused memory after extensive `pop()` or `clear()` operations, call `queue.shrink()`.
affects: >=1.2.0
gotchaFlatQueue is not 'stable' for items with identical priority values. If multiple items share the same lowest priority, there is no guarantee which of them will be returned first by `pop()` or `peek()`.
fix
If deterministic ordering for same-priority items is required, you must incorporate a secondary sorting criterion into your priority value (e.g., by combining priority with a stable index) or use a different priority queue implementation.
affects: >=1.0.0
gotchaFor maximum performance and memory efficiency, especially when the maximum queue size is known, you can override the internal `ids` and `values` arrays with typed arrays (e.g., `Uint16Array`, `Uint32Array`).
fix
Before pushing items, initialize `q.ids = new Uint16Array(maxSize);` and `q.values = new Uint32Array(maxSize);`.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use CommonJS `require()` to import `flatqueue` after it became an ESM-only package.
fix
Update your import statement to `import FlatQueue from 'flatqueue';` and ensure your project's `package.json` specifies `"type": "module"` if running in Node.js, or use a bundler configured for ESM.
Perceived 'memory leak' or unexpectedly high memory usage over time.
The internal `ids` and `values` arrays do not automatically resize down after `pop()` or `clear()` operations to improve performance by avoiding frequent reallocations.
fix
Call `queue.shrink()` manually to trim the internal arrays to the current `length` of the queue, freeing unused memory. This should be done when memory usage is a concern or after significant reduction in queue size.
TypeError: Cannot read properties of undefined (reading 'id') (or similar for other properties)
Attempting to access properties of the return value from `peek()` or `pop()` when the queue is empty. Both methods return `undefined` when the queue contains no items.
fix
Always check if the return value of `peek()` or `pop()` is `undefined` before attempting to access its properties. For example: `const item = q.pop(); if (item !== undefined) { console.log(item.id); }`
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
flatqueue — npm install flatqueue · libregistry