Registry /
data-structures / priority-queue-typescript
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.
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.
The package exports a named class, not a default export. This applies to both TypeScript and JavaScript (ESM).
import { PriorityQueue } from 'priority-queue-typescript'
When using CommonJS (require), destructure the named export. The package provides a CJS bundle as default.
const { PriorityQueue } = require('priority-queue-typescript')
Types are included in the main entry. No separate type import needed.
import { PriorityQueue } from 'priority-queue-typescript';
// Uses built-in types automatically
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
Upgrade
Version history
Breaking-change detection hasn't run for this library yet.
Audit
Security & dependencies
CVE tracking and dependency tree are planned for a later release.