Registry / devops / priorityqueue

priorityqueue

JSON →
library2.1.0jsnpmunverified

PriorityQueue is a TypeScript-friendly JavaScript library implementing priority queues with multiple heap variants (BinaryHeap, PairingHeap, SkewHeap). Version 2.1.0 is current; updates are occasional. Key differentiators: supports custom comparators, offers three heap implementations with different performance trade-offs (BinaryHeap for general use, PairingHeap and SkewHeap for fast merge operations), exports ESM and includes TypeScript definitions. Suitable for algorithmic tasks, scheduling, and graph algorithms.

npm install priorityqueue
INSTALL
IMPORT
SIG · PRIORITYQUEUE
P
priorityqueue
devopsjavascriptv2.1.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
import PriorityQueue from 'priorityqueue'
const PriorityQueue = require('priorityqueue')
ESM-only; CommonJS require is not supported. Use dynamic import if needed.
BinaryHeap
import BinaryHeap from 'priorityqueue/BinaryHeap'
import BinaryHeap from 'priorityqueue/lib/BinaryHeap'
For TypeScript with moduleResolution 'node', use 'priorityqueue/lib/BinaryHeap' instead.
PairingHeap
import PairingHeap from 'priorityqueue/PairingHeap'
import PairingHeap from 'priorityqueue/lib/PairingHeap'
Same note as BinaryHeap regarding TypeScript moduleResolution.
SkewHeap
import SkewHeap from 'priorityqueue/SkewHeap'
import SkewHeap from 'priorityqueue/lib/SkewHeap'
TypeScript users with moduleResolution 'node' must use the 'lib/' subpath.

Shows basic usage with number and custom object comparators, push, pop, top, and length.

import PriorityQueue from 'priorityqueue'; const pq = new PriorityQueue({ comparator: (a, b) => a - b }); pq.push(4); pq.push(2); pq.push(5); pq.push(1); console.log(pq.pop()); // 5 console.log(pq.top()); // 4 console.log(pq.length); // 3 // Custom objects class Point { constructor(x, y) { this.x = x; this.y = y; } } const pointComparator = (a, b) => (a.x !== b.x ? a.x - b.x : a.y - b.y); const pq2 = new PriorityQueue({ comparator: pointComparator }); pq2.push(new Point(4, 6)); pq2.push(new Point(2, 3)); pq2.push(new Point(5, 1)); console.log(pq2.pop()); // Point { x: 5, y: 1 }
Debug
Known issues
breakingIn version 2, import paths changed from 'priorityqueue/lib/...' to 'priorityqueue/...' for TypeScript users. The old paths still work for CJS but the ESM exports use the new paths.
fix
Update imports from 'priorityqueue/lib/BinaryHeap' to 'priorityqueue/BinaryHeap' (unless using moduleResolution 'node', then keep 'lib/').
affects: >=2.0.0
breakingVersion 2 replaced the default export with a class factory. Previously 'new PriorityQueue()' accepted no comparator; now a comparator option is required.
fix
Provide a comparator function in the options object: new PriorityQueue({ comparator: (a,b) => a - b }).
affects: >=2.0.0
deprecatedThe static methods like PriorityQueue.merge() are deprecated in version 2. Use instance methods instead.
fix
Use pq.merge(otherHeap) for merging heaps.
affects: >=2.0.0
gotchaThe default export PriorityQueue is an alias for BinaryHeap. If you import a specific heap like PairingHeap, it does not share the same static methods as PriorityQueue.
fix
Use the specific heap class directly: import PairingHeap from 'priorityqueue/PairingHeap'.
affects: >=2.0.0
gotchaTypeScript users with tsconfig moduleResolution 'node' must use paths like 'priorityqueue/lib/BinaryHeap' instead of 'priorityqueue/BinaryHeap'. Otherwise, imports will fail.
fix
Change imports to 'priorityqueue/lib/...' or switch moduleResolution to 'node16' or 'bundler'.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: PriorityQueue is not a constructor
CommonJS require used instead of ESM import.
fix
Use import PriorityQueue from 'priorityqueue' in an ESM context.
Cannot find module 'priorityqueue/BinaryHeap' or its corresponding type declarations.
TypeScript moduleResolution set to 'node' and using non-lib path.
fix
Import from 'priorityqueue/lib/BinaryHeap' instead, or change moduleResolution to 'node16' or 'bundler'.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
priorityqueue — npm install priorityqueue · libregistry