Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mourner/tinyqueue
The smallest and simplest priority queue in JavaScript.
https://github.com/mourner/tinyqueue
algorithm binary-heap data-structure javascript priority-queue
Last synced: 7 days ago
JSON representation
The smallest and simplest priority queue in JavaScript.
- Host: GitHub
- URL: https://github.com/mourner/tinyqueue
- Owner: mourner
- License: isc
- Created: 2015-04-23T16:23:18.000Z (almost 10 years ago)
- Default Branch: main
- Last Pushed: 2024-07-06T08:59:34.000Z (7 months ago)
- Last Synced: 2025-01-08T21:43:36.358Z (15 days ago)
- Topics: algorithm, binary-heap, data-structure, javascript, priority-queue
- Language: JavaScript
- Size: 37.1 KB
- Stars: 440
- Watchers: 14
- Forks: 46
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## tinyqueue
The smallest and simplest binary heap priority queue in JavaScript.
```js
// create an empty priority queue
let queue = new TinyQueue();// add some items
queue.push(7);
queue.push(5);
queue.push(10);// remove the top item
let top = queue.pop(); // returns 5// return the top item (without removal)
top = queue.peek(); // returns 7// get queue length
queue.length; // returns 2// create a priority queue from an existing array (modifies the array)
queue = new TinyQueue([7, 5, 10]);// pass a custom item comparator as a second argument
queue = new TinyQueue([{value: 5}, {value: 7}], function (a, b) {
return a.value - b.value;
});// turn a queue into a sorted array
const array = [];
while (queue.length) array.push(queue.pop());
```For a faster number-based queue, see [flatqueue](https://github.com/mourner/flatqueue).
### Install
Install using NPM (`npm install tinyqueue`), then import as a module:
```js
import TinyQueue from 'tinyqueue';
```Or use a browser build from a CDN:
```html
import TinyQueue from 'https://cdn.jsdelivr.net/npm/tinyqueue/+esm';
```### Thanks
Inspired by [js-priority-queue](https://github.com/adamhooper/js-priority-queue)
by [Adam Hooper](https://github.com/adamhooper).