Registry / storage / priorityqueuejs

priorityqueuejs

JSON →
library2.0.0jsnpmunverified

A simple priority queue (binary heap) implementation for Node.js and browsers. Stable version 2.0.0 (latest release); no longer actively developed but functional. It provides a straightforward API with custom comparator support and default comparator for numbers/strings. Differentiator: minimalistic, no dependencies, suitable for small projects. However, it lacks TypeScript types and modern ESM support, and the stable API has been unchanged for years.

npm install priorityqueuejs
INSTALL
IMPORT
SIG · PRIORITYQUEUEJS
P
priorityqueuejs
storagejavascriptv2.0.0
harness data pending
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.

PriorityQueue
var PriorityQueue = require('priorityqueuejs');
import PriorityQueue from 'priorityqueuejs';
The package exports a single constructor via CommonJS. There is no default ESM export; using ES import will result in undefined.
DEFAULT_COMPARATOR
PriorityQueue.DEFAULT_COMPARATOR
const { DEFAULT_COMPARATOR } = require('priorityqueuejs');
DEFAULT_COMPARATOR is a static method on the constructor, not a separate export. Destructuring from require will give undefined.
PriorityQueue (with custom comparator)
var q = new PriorityQueue(function(a, b) { return a - b; });
var q = new PriorityQueue((a, b) => a - b);
Arrow functions work fine, but ensure the comparator returns a number. Common mistake: returning a boolean.

Creates a priority queue with a custom comparator for objects, enqueues three items, and demonstrates peek, deq, and size.

var PriorityQueue = require('priorityqueuejs'); var queue = new PriorityQueue(function(a, b) { return a.cash - b.cash; }); queue.enq({ cash: 250, name: 'Valentina' }); queue.enq({ cash: 300, name: 'Jano' }); queue.enq({ cash: 150, name: 'Fran' }); console.log(queue.size()); // 3 console.log(queue.peek()); // { cash: 300, name: 'Jano' } console.log(queue.deq()); // { cash: 300, name: 'Jano' } console.log(queue.size()); // 2
Debug
Known issues
gotchaThe comparator function must return a positive number when a > b, 0 when a == b, and a negative number when a < b. A common mistake is returning a boolean.
fix
Ensure the comparator returns a number, e.g., a - b for numbers.
affects: >=1.0.0
deprecatedThe package is no longer actively maintained; no new releases since 2016. No TypeScript definitions, no ESM support.
fix
Consider using modern alternatives like @datastructures-js/priority-queue or heap.
affects: >=2.0.0
gotchaforEach executes synchronously and does not trigger reordering; mutating priorities during iteration can break heap invariance.
fix
Avoid modifying elements inside forEach. If you must, recreate the queue after changes.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: PriorityQueue is not a constructor
Using ES import syntax (import PriorityQueue from 'priorityqueuejs') which returns undefined because package has no default export.
fix
Use require: var PriorityQueue = require('priorityqueuejs');
TypeError: __WEBPACK_DEFAULT_EXPORT__ is not a constructor
Webpack's default export handling with CommonJS module expecting default export.
fix
Use require or configure webpack to handle CommonJS modules.
Uncaught Error: Empty queue
Calling deq() or peek() on an empty priority queue.
fix
Check isEmpty() before calling deq() or peek().
Expected behavior: queue does not sort correctly
Comparator returns boolean (true/false) instead of a number.
fix
Ensure comparator returns a numeric value: return a - b (for ascending) or b - a (for descending).
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
25 hits · last 30 days
node
24
Amazon
1
Resources
priorityqueuejs — npm install priorityqueuejs · libregistry