Registry / devops / p-queue-ts

p-queue-ts

JSON →
library1.2.0jsnpmunverified

Minimalist priority queue implementation using a binary heap, written in TypeScript. Version 1.2.0 ships with type definitions and supports both max-heap (default) and min-heap via custom comparator. The library is small (under 1KB gzipped), has zero dependencies, and targets Node >=8. It provides a basic API (push, pop, top, size) common to LeetCode-style problems. Compared to alternatives like @datastructures-js/priority-queue or js-priority-queue, this package is simpler and less feature-rich, but it remains actively maintained as of 2025.

npm install p-queue-ts
INSTALL
IMPORT
SIG · P-QUEUE-TS
P
p-queue-ts
devopsjavascriptv1.2.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 'p-queue-ts'
const PriorityQueue = require('p-queue-ts')
ESM import is the recommended way; CommonJS require works but returns the object with PriorityQueue as a property.
PriorityQueue (default)
import PriorityQueue from 'p-queue-ts'
import queue from 'p-queue-ts'
There is no default export; use named import or 'import * as queue from ...'.
PriorityQueue (type)
import type { PriorityQueue } from 'p-queue-ts'
import { PriorityQueueType } from 'p-queue-ts'
TypeScript users can import the class as a type if only using it for type annotations.

Demonstrates max-heap and min-heap usage with numbers and objects, including push, pop, top, and size methods.

import { PriorityQueue } from 'p-queue-ts'; // Max-heap (default) const maxPQ = new PriorityQueue<number>(); maxPQ.push(3); maxPQ.push(1); maxPQ.push(4); maxPQ.push(1); maxPQ.push(5); console.log(maxPQ.pop()); // 5 console.log(maxPQ.top()); // 4 console.log(maxPQ.size()); // 4 // Min-heap with custom comparator const minPQ = new PriorityQueue<number>((a, b) => a > b); minPQ.push(3); minPQ.push(1); minPQ.push(4); console.log(minPQ.pop()); // 1 // Object priority const objPQ = new PriorityQueue<{id: number, priority: number}>( (a, b) => a.priority < b.priority ); objPQ.push({ id: 1, priority: 5 }); objPQ.push({ id: 2, priority: 1 }); console.log(objPQ.pop()); // { id: 2, priority: 1 }
Debug
Known issues
gotchaThe constructor comparator signature is (a, b) => boolean. For min-heap, use a > b; for max-heap, the default is a < b (or omit).
fix
If you want a min-heap of numbers, pass (a, b) => a > b. For objects, compare the priority field appropriately.
affects: >=1.0.0
gotchapop() returns the highest-priority element and removes it. There is no method to peek at the last element.
fix
Use top() to get the highest-priority element without removal.
affects: >=1.0.0
gotchaThe queue does not support updating priorities. Once an element is inserted, its priority cannot be changed.
fix
If you need dynamic priority updates, consider using a different library like @datastructures-js/priority-queue.
affects: >=1.0.0
gotchasize() is a function, not a property. Common mistake: accessing .size instead of .size() returns undefined.
fix
Call size() as a method, e.g., const len = pq.size();
affects: >=1.0.0
gotchaThe comparator must return a boolean, not a number. Passing (a, b) => a - b will not work correctly.
fix
Use (a, b) => a > b for min-heap or (a, b) => a < b for max-heap (default).
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: pq.size is not a function
Attempting to access size as a property instead of a method.
fix
Replace pq.size with pq.size()
The comparator must return a boolean when comparing
Passing a numeric comparator like (a, b) => a - b, which returns a number.
fix
Use (a, b) => a > b (for min-heap) or (a, b) => a < b (for max-heap).
Cannot find module 'p-queue-ts'
Package not installed or wrong import path.
fix
Run 'npm install p-queue-ts' and use correct import: import { PriorityQueue } from 'p-queue-ts'
Uncaught TypeError: Class extends value undefined is not a constructor or null
Importing the package incorrectly (e.g., using default import when there's no default export).
fix
Use named import: import { PriorityQueue } from 'p-queue-ts'
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
p-queue-ts — npm install p-queue-ts · libregistry