FastPriorityQueue.js is a high-performance, heap-based priority queue implementation for JavaScript and TypeScript. It provides efficient O(log n) operations for adding and polling elements, and O(1) for peeking the smallest element. The package is currently at version 0.8.0 and maintains an active development status, with recent minor releases addressing bug fixes and performance optimizations. Its primary differentiator is its focus on raw performance, often outperforming other JavaScript priority queue libraries by a significant margin, making it ideal for applications where the speed of data structure operations is critical. It ships with TypeScript types, ensuring a robust developer experience in typed environments.
npm install fastpriorityqueueVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the core functionality of FastPriorityQueue, including adding elements, peeking at the top element, checking the queue size, and polling elements. It also illustrates how to create a max-priority queue using a custom comparator function.
Thoroughly test custom comparator functions with diverse data sets and edge cases to ensure the expected ordering. Remember that the comparator should return `true` if `a` has higher priority (comes before) `b`.
If an element's priority changes, it must be explicitly removed from the queue (`remove()` or `removeOne()`), its value updated, and then re-added (`add()`) to ensure the queue maintains its heap property. The `replaceTop()` method can be used if the mutated element was the top element.
Users relying on `kSmallest` functionality should upgrade to version `0.7.4` or newer to resolve this correctness issue and ensure accurate results.
If precise removal of a specific object instance is required, use `removeOne(callback)` and provide a callback function that performs a strict identity check (e.g., `(item) => item === mySpecificObject`).
Consider calling `pq.trim()` after periods of significant queue modification to reclaim unused memory and optimize the heap's underlying storage.
Always instantiate the queue using `new FastPriorityQueue()`. For ESM/TypeScript, use `import FastPriorityQueue from 'fastpriorityqueue';`. For CommonJS, use `const FastPriorityQueue = require('fastpriorityqueue');`.First, ensure the package is installed: `npm install fastpriorityqueue`. Then, verify your import/require statement matches your module system (e.g., `import FastPriorityQueue from 'fastpriorityqueue';` for ESM/TypeScript or `const FastPriorityQueue = require('fastpriorityqueue');` for CommonJS).Review your comparator function to ensure it correctly defines the priority (e.g., `(a, b) => a > b` for a max-heap, `(a, b) => a < b` or default for a min-heap). If using objects, ensure they are not mutated after being added to the queue; if their priority changes, remove and re-add them.
No dependency data recorded yet.