npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

js-pq

v1.0.0

Published

A performant generic Priority Queue implementation using Binary Heap

Readme

js-pq

A high-performance generic Priority Queue implementation for JavaScript and TypeScript using a Binary Heap.

This project was developed for educational purposes to analyze and understand the state of priority queue implementations in JavaScript. I found the ecosystem mature and well-established.

Integration Considerations:

  • If you require extreme performance and can work within its integer-only constraints, Heapify is the optimal choice.
  • FlatQueue is an excellent option for managing objects where a custom comparator is not strictly required.
  • FastPriorityQueue remains slightly faster for pure numeric data.
  • TinyQueue offers a smaller codebase, which means smaller bundle size.
  • heap-js is more feature-rich for advanced heap manipulations.

js-pq is for general-purpose applications that need a balance of performance and the flexibility of custom comparators.

License NPM Version

Table of Contents

Features

  • 🚀 High Performance: Built with performance in mind, competitive with top-tier libraries like FastPriorityQueue.
  • 🛠️ Generic Support: Handles any data type—numbers, strings, or complex objects—with a custom comparator.
  • ⚡ Efficient Operations: O(log n) push/pop, O(1) peek, and O(n) heapify.
  • 📦 Zero Dependencies: Lightweight and easy to integrate.
  • TypeScript Support: Full type definitions included out of the box.

Installation

npm install js-pq

Usage

Basic Usage (Min-Heap)

By default, the queue acts as a Min-Heap for numbers.

import PriorityQueue from 'js-pq';

const pq = new PriorityQueue();
pq.push(10);
pq.push(5);
pq.push(20);

console.log(pq.peek()); // 5
console.log(pq.pop());  // 5
console.log(pq.size()); // 2

Advanced Usage (Max-Heap with Objects)

Use a custom comparator to manage complex objects. The comparator should return true if the first element has higher priority than the second.

const maxHeap = new PriorityQueue((a, b) => a.priority > b.priority);

maxHeap.push({ task: 'low', priority: 1 });
maxHeap.push({ task: 'high', priority: 10 });
maxHeap.push({ task: 'medium', priority: 5 });

console.log(maxHeap.pop()); // { task: 'high', priority: 10 }

O(n) Heapify

Construct a heap from an existing array efficiently.

const items = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7];
const pq = new PriorityQueue();
pq.heapify(items);

console.log(pq.pop()); // 1

API Documentation

new PriorityQueue(comparator?)

Creates a new priority queue.

  • comparator: (Optional) A function (a, b) => boolean. Returns true if a should come before b.

.push(item)

Adds an item to the queue. Returns the new size.

  • Time Complexity: O(log n)

.pop()

Removes and returns the item with the highest priority.

  • Time Complexity: O(log n)

.peek()

Returns the item with the highest priority without removing it.

  • Time Complexity: O(1)

.size()

Returns the number of items in the queue.

  • Time Complexity: O(1)

.isEmpty()

Returns true if the queue is empty.

  • Time Complexity: O(1)

.heapify(array)

Replaces the queue content with the provided array and builds a heap in place.

  • Time Complexity: O(n)

Performance Comparison

Benchmarks performed on 1,000,000 random numbers (Total time for Push + Pop ops in ms):

| Library | Push + Pop (1M) | Type | Best For | |---|---|---|---| | js-pq | 114.81ms | Binary Heap | General use, high performance | | FastPriorityQueue | 112.08ms | Binary Heap | High performance, specific focus on numbers | | TinyQueue | 126.66ms | Binary Heap | Small code size, minimal API | | heap-js | 134.15ms | Binary Heap | Feature-rich API (extract, replace, etc.) | | FlatQueue | 129.66ms | Binary Heap | Optimized for complex object handling | | Heapify | 6.44ms | Integer Heap | Extreme performance for integers/indexes |

License

MIT