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.

data-structures
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
Debug
Known footguns
breakingConstructor changed between v1 and v2: first parameter is now initial capacity (number) instead of always 11.
gotchapoll() returns null when the queue is empty, not undefined or throws an error.
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.
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.

Agent activity
8 hits · last 30 days
gptbot
3
ahrefsbot
1
Resources