Registry / data-structures / priority-queue-typescript

priority-queue-typescript

JSON →
library2.0.3jsnpmunverified

A priority queue (binary heap) implementation in TypeScript with full type support. The constructor accepts an initial capacity and a custom comparator function, defaulting to max-heap. It supports add, poll, peek, contains, clear, toArray, iterable protocol, and dynamic resizing. Version 2.0.3 is the latest stable release; the package is actively maintained. Key differentiators: simple API, TypeScript-first, no runtime dependencies, supports ESM and CJS via CommonJS. Suitable for algorithmic tasks, stream processing, or any in-memory sorting needs.

npm install priority-queue-typescript
INSTALL
IMPORT
SIG · PRIORITY-QUEUE-TYP
P
priority-queue-typescript
data-structuresjavascriptv2.0.3
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 'priority-queue-typescript'
import PriorityQueue from 'priority-queue-typescript'
The package exports a named class, not a default export. This applies to both TypeScript and JavaScript (ESM).
PriorityQueue (CommonJS)
const { PriorityQueue } = require('priority-queue-typescript')
const PriorityQueue = require('priority-queue-typescript')
When using CommonJS (require), destructure the named export. The package provides a CJS bundle as default.
PriorityQueue type (TypeScript)
import { PriorityQueue } from 'priority-queue-typescript'; // Uses built-in types automatically
import { PriorityQueue } from 'priority-queue-typescript/types'
Types are included in the main entry. No separate type import needed.

Shows creation of max-heap, min-heap with custom comparator, custom object usage, iteration, and size method.

import { PriorityQueue } from 'priority-queue-typescript'; // Max-heap (default): largest priority first const maxQueue = new PriorityQueue<number>(); maxQueue.add(10); maxQueue.add(100); maxQueue.add(9); console.log(maxQueue.poll()); // 100 // Min-heap: smallest priority first const minQueue = new PriorityQueue<number>( 10, (a: number, b: number) => a - b ); minQueue.add(10); minQueue.add(100); minQueue.add(9); console.log(minQueue.poll()); // 9 // With custom objects interface Task { priority: number; name: string; } const taskQueue = new PriorityQueue<Task>( 5, (a: Task, b: Task) => b.priority - a.priority ); taskQueue.add({ priority: 3, name: 'low' }); taskQueue.add({ priority: 5, name: 'high' }); console.log(taskQueue.poll()?.name); // 'high' // Iterate without removing for (const item of minQueue) { console.log(item); } console.log(minQueue.size()); // remaining size
Debug
Known issues
breakingConstructor changed between v1 and v2: first parameter is now initial capacity (number) instead of always 11.
fix
If migrating from v1, pass initial capacity explicitly. v2 (current) uses capacity >0; default is 11 if omitted.
affects: >=1.0.0 <2.0.0
gotchapoll() returns null when the queue is empty, not undefined or throws an error.
fix
Always check for null after poll() when queue might be empty.
affects: >=1.0.0
gotchaComparing numbers: default comparator is (a: T, b: T) => a < b ? 1 : -1. For numbers, this gives highest value first (max-heap). To invert, use (a, b) => a - b for min-heap of numbers.
fix
For max-heap: use default or (a, b) => b - a. For min-heap: use (a, b) => a - b.
affects: >=1.0.0
Errors
Common errors & fixes
Uncaught TypeError: PriorityQueue is not a constructor
Importing the default export instead of named export.
fix
Change 'import PriorityQueue from ...' to 'import { PriorityQueue } from ...'
TypeError: queue.poll is not a function
Instance created with wrong constructor or queue is undefined.
fix
Ensure you use 'new PriorityQueue()' and that the variable is properly assigned.
Error: heap full
Queue initialized with capacity 0 or negative, then add() called without capacity check.
fix
Use initial capacity >= 1; e.g., new PriorityQueue<number>(10). Queue auto-grows but capacity 0 prevents initial add.
Upgrade
Version history
2.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Amazon
1
Resources
priority-queue-typescript — npm install priority-queue-typescript · libregistry