# priorityqueue

> An implementation of Priority Queue

Latest version **2.1.0** (published 2025-10-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install priorityqueue
pnpm add priorityqueue
yarn add priorityqueue
bun add priorityqueue
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2025-10-02 |
| First published | 2015-07-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 60.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | berlysia |
| Maintainers | berlysia |
| Keywords | priority, queue, priority queue, heap, data structure |

## Links

- npm: https://www.npmjs.com/package/priorityqueue
- Repository: https://github.com/berlysia/priorityqueuejs
- Homepage: https://github.com/berlysia/priorityqueuejs#readme
- Issues: https://github.com/berlysia/priorityqueuejs/issues
- npm.io page: https://npm.io/package/priorityqueue

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 2.1.0 (latest) — 2025-10-02
- 2.0.0-0 (alpha) — 2022-11-04
- 1.0.0-rc3.0 (next) — 2019-10-02
- 2.0.0 — 2022-11-04
- 1.0.0 — 2019-10-02
- 1.0.0-rc3 — 2019-09-16
- 1.0.0-rc2 — 2019-09-15
- 1.0.0-rc1 — 2018-02-25
- 0.2.1 — 2018-02-22
- 0.2.0 — 2016-05-06
- 0.1.0 — 2015-08-02
- 0.0.4 — 2015-08-01
- 0.0.3 — 2015-08-01
- 0.0.2 — 2015-08-01
- 0.0.1 — 2015-07-31

## README

# PriorityQueue

An implementation of priority queue in javascript.

## Installation

```
npm install priorityqueue
```

## Example

```js
import PriorityQueue from "priorityqueue";

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}

const numericCompare = (a, b) => (a > b ? 1 : a < b ? -1 : 0);

const comparator = (a, b) => {
  const x = numericCompare(a.x, b.x);
  const y = numericCompare(a.y, b.y);
  return x ? x : y;
};

const pq = new PriorityQueue({ comparator });

pq.push(new Point(4, 6));
pq.push(new Point(2, 3));
pq.push(new Point(5, 1));
pq.push(new Point(1, 2));
console.log(pq.pop()); // => {x: 5, y: 1}
console.log(pq.top()); // => {x: 4, y: 6}
pq.push(new Point(3, 4));
pq.push(new Point(6, 5));
console.log(pq.length); // => 5
console.log(pq.top()); // => {x: 6, y: 5}
```

## References

- [instance methods](http://berlysia.github.io/priorityqueuejs/types/index.PriorityQueueInstance.html)
- [static methods](http://berlysia.github.io/priorityqueuejs/types/index.PriorityQueueStatic.html)

## Variation

## BinaryHeap(default)

Binary heap is a simple and efficient in almost cases.

cons:

- slow with large amount of items(over 10k)
- slow in `merge` operation especially

## PairingHeap

pros:

- super fast in `merge` operation(constant time)

## SkewHeap

pros:

- super fast in `merge` operation(constant time)

Not to use:

- with sequence completely sorted

## Import specific implementation

```js
import PriorityQueue from "priorityqueue";
import BinaryHeap from "priorityqueue/BinaryHeap";
import PairingHeap from "priorityqueue/PairingHeap";
import SkewHeap from "priorityqueue/SkewHeap";

console.log(PriorityQueue === BinaryHeap); // => true
```

### TypeScript users

If your tsconfig specifies `moduleResolution: "node"` (whether implicitly or explicitly), use `priorityqueue/lib/BinaryHeap` instead of `priorityqueue/BinaryHeap` .

---
_Source: https://npm.io/package/priorityqueue · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
