Registry / storage / min-priority-queue-typed

min-priority-queue-typed

JSON →
library2.6.0jsnpmunverified

A TypeScript implementation of a min-priority queue data structure, extracted from the data-structure-typed collection. Version 2.6.0 offers a standalone binary heap-based priority queue with a customizable comparator for ordering elements. The package ships TypeScript declarations and supports ESM and CJS via module bundlers. It differentiates itself from built-in JavaScript priority queues by providing a dedicated min-heap with explicit add, poll, and peek methods, making it suitable for scheduling, event simulation, and algorithmic use cases. The library is actively maintained with regular releases.

npm install min-priority-queue-typed
INSTALL
IMPORT
SIG · MIN-PRIORITY-QUEUE
M
min-priority-queue-typed
storagejavascriptv2.6.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.

MinPriorityQueue
import { MinPriorityQueue } from 'min-priority-queue-typed'
import MinPriorityQueue from 'min-priority-queue-typed'
Since v1, only named export is supported. Default import will fail in TypeScript.
MinPriorityQueue (require)
const { MinPriorityQueue } = require('min-priority-queue-typed')
const MinPriorityQueue = require('min-priority-queue-typed')
CommonJS requires destructuring the named export.
MinPriorityQueue type (TypeScript)
import type { MinPriorityQueue } from 'min-priority-queue-typed'
import { MinPriorityQueue } from 'min-priority-queue-typed'
For type-only imports (e.g., in .d.ts or when using as a type), use `import type`.

Demonstrates basic usage: creating a min-priority queue, adding elements, peeking, polling, and using a custom comparator for objects.

import { MinPriorityQueue } from 'min-priority-queue-typed'; // Create a min priority queue for numbers (default comparator is min-heap) const pq = new MinPriorityQueue<number>(); // Add elements pq.add(5); pq.add(1); pq.add(3); // Peek at the smallest element without removing console.log(pq.peek()); // 1 // Poll: remove and return the smallest element console.log(pq.poll()); // 1 console.log(pq.poll()); // 3 console.log(pq.poll()); // 5 // Check size console.log(pq.size); // 0 // Use custom comparator for objects interface Task { priority: number; name: string; } const taskQueue = new MinPriorityQueue<Task>([], { comparator: (a, b) => a.priority - b.priority }); taskQueue.add({ priority: 3, name: 'Medium' }); taskQueue.add({ priority: 1, name: 'High' }); taskQueue.add({ priority: 5, name: 'Low' }); console.log(taskQueue.poll()?.name); // 'High' console.log(taskQueue.poll()?.name); // 'Medium' console.log(taskQueue.poll()?.name); // 'Low'
Debug
Known issues
gotchaThe constructor's first argument expects an initial array of elements, not options. Passing options without the initial array will cause runtime errors.
fix
Use `new MinPriorityQueue<T>([], { comparator: ... })` to pass options with an empty array.
affects: >=1.0.0
gotchaThe `comparator` option is required if elements are not numbers/strings that naturally compare. If omitted, the default comparator assumes `a < b` which may not work for objects.
fix
Always provide a comparator function for non-primitive types.
affects: >=1.0.0
gotchaThe `peek` method returns `undefined` when the queue is empty, not throwing an error. This can mask bugs if not checked.
fix
Always check `queue.size > 0` before calling `peek()` or use optional chaining.
affects: >=1.0.0
deprecatedThe `min-priority-queue-typed` package is a subset of the `data-structure-typed` monorepo. Future development may be consolidated into the main package.
fix
Consider migrating to `data-structure-typed` for access to all data structures.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: MinPriorityQueue is not a constructor
Using default import instead of named import.
fix
Change to `import { MinPriorityQueue } from 'min-priority-queue-typed'`.
Uncaught TypeError: Cannot read properties of undefined (reading 'comparator')
Passing options object as first argument without an empty array.
fix
Use `new MinPriorityQueue([], { comparator: ... })`.
TypeError: pq.peek is not a function
The package exported incorrectly or an older version without `peek` method.
fix
Update to version >=1.0.0 and ensure correct import.
Upgrade
Version history
2.6.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources
min-priority-queue-typed — npm install min-priority-queue-typed · libregistry