{"id":13447586,"url":"https://github.com/klaudiosinani/prioqueue","last_synced_at":"2025-06-15T02:37:54.386Z","repository":{"id":57330573,"uuid":"186061390","full_name":"klaudiosinani/prioqueue","owner":"klaudiosinani","description":"Priority queues for ES6","archived":false,"fork":false,"pushed_at":"2019-10-05T12:45:18.000Z","size":94,"stargazers_count":24,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-03T11:29:24.205Z","etag":null,"topics":["es6","priority","queue","typescript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/klaudiosinani.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"contributing.md","funding":null,"license":"license.md","code_of_conduct":"code-of-conduct.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-10T22:21:46.000Z","updated_at":"2022-10-20T13:00:02.000Z","dependencies_parsed_at":"2022-09-09T08:21:13.641Z","dependency_job_id":null,"html_url":"https://github.com/klaudiosinani/prioqueue","commit_stats":null,"previous_names":["klaussinani/prioqueue"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/klaudiosinani/prioqueue","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fprioqueue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fprioqueue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fprioqueue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fprioqueue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/klaudiosinani","download_url":"https://codeload.github.com/klaudiosinani/prioqueue/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fprioqueue/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259913601,"owners_count":22931242,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["es6","priority","queue","typescript"],"created_at":"2024-07-31T05:01:21.740Z","updated_at":"2025-06-15T02:37:54.363Z","avatar_url":"https://github.com/klaudiosinani.png","language":"JavaScript","readme":"\u003ch1 align=\"center\"\u003e\n  Prioqueue\n\u003c/h1\u003e\n\n\u003ch4 align=\"center\"\u003e\n  Priority queues for ES6\n\u003c/h4\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://travis-ci.com/klaussinani/prioqueue\"\u003e\n    \u003cimg alt=\"Build Status\" src=\"https://travis-ci.com/klaussinani/prioqueue.svg?branch=master\"\u003e\n  \u003c/a\u003e\n  \u003ca href='https://coveralls.io/github/klaussinani/prioqueue?branch=master'\u003e\n    \u003cimg alt=\"Coverage Status\" src=\"https://coveralls.io/repos/github/klaussinani/prioqueue/badge.svg?branch=master\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n## Description\n\nES6 implementation of the priority queue data structure with TypeScript support.\n\nVisit the [contributing guidelines](https://github.com/klaussinani/prioqueue/blob/master/contributing.md#translating-documentation) to learn more on how to translate this document into more languages.\n\n## Contents\n\n- [Description](#description)\n- [Install](#install)\n- [In Depth](#in-depth)\n- [Development](#development)\n- [Related](#related)\n- [Team](#team)\n- [License](#license)\n\n## Install\n\n### Yarn\n\n```bash\nyarn add prioqueue\n```\n\n### NPM\n\n```bash\nnpm install prioqueue\n```\n\n## In Depth\n\nA priority queue is an abstract queue type, similar to a regular queue or stack data structure, but where additionally each item has a `priority` associated with it. In a priority queue, an item with high priority is served before an item with low priority. To improve performance, Prioqueue priority queues use an array implemented binary heap as their backbone, giving `O(log n)` performance for inserts and removals.\n\n## Usage\n\nPrioqueue exposes a chainable API, that can be utilized through a simple and minimal syntax, allowing you to combine methods effectively.\n\nTo create a **max-priority queue**, where items are inserted  in the order in which they arrive and the item with the maximum priority value is always served first, we provide as argument to the `Queue` class, on instantiation, a binary comparator function `compareMax(x, y)`, which returns a positive number when the priority of item `x` is greater than the one of item `y`, zero when equal and a negative number when less than.\n\nAccordingly, to create a **min-priority queue**, where items are inserted in the order in which they arrive and the item with the minimum priority value is always served first, a binary comparator function `compareMin(x, y)` must be passed as argument, which returns a positive number when the priority of item `x` is less than the one of item `y`, zero when equal and a negative number when greater than.\n\nBy default, if no comparator function is provided on instantiation, a **max-priority queue** instance is returned.\n\nUsage examples can be also found at the [`test`](https://github.com/klaussinani/prioqueue/tree/master/test) directory.\n\n```js\n'use strict';\nconst {Queue, Item} = require('prioqueue');\n\n// Create a max priority queue\nconst maxQueue = new Queue((x, y) =\u003e x.priority - y.priority);\n//=\u003e Queue { queue: [] }\n\nmaxQueue.enqueue(15, 'A');\n//=\u003e Queue { queue: [Item { priority: 15, value: 'A' }] }\n\nmaxQueue.peek();\n//=\u003e Item { priority: 15, value: 'A' }\n\nconst item = new Item(15, 'A');\n\nmaxQueue.peek().toPair();\n//=\u003e [15, 'A']\n\nmaxQueue.peekPriority() === item.priority;\n//=\u003e true\n\nmaxQueue.peekValue() === item.value;\n//=\u003e true\n\nmaxQueue.enqueue(10, 'B').enqueue(5, 'C');\n//=\u003e Queue { queue: [\n// Item { priority: 15, value: 'A' },\n// Item { priority: 10, value: 'B' },\n// Item { priority: 5, value: 'C' } ] }\n\nmaxQueue.includes('A');\n//=\u003e true\n\nmaxQueue.includes('D');\n//=\u003e false\n\nmaxQueue.enqueue(7, 'D').enqueue(8, 'E').enqueue(2, 'F');\n//=\u003e Queue { queue: [\n// Item { priority: 15, value: 'A' },\n// Item { priority: 10, value: 'B' },\n// Item { priority: 5, value: 'C' },}\n// Item { priority: 7, value: 'D' },\n// Item { priority: 8, value: 'E' },\n// Item { priority: 2, value: 'F' } ] }\n\nmaxQueue.search('E');\n//=\u003e Item { priority: 8, value: 'E' }\n\nmaxQueue.dequeue();\n//=\u003e Item { priority: 15, value: 'A' }\n\nmaxQueue.dequeue();\n//=\u003e Item { priority: 10, value: 'B' }\n\nmaxQueue.peek();\n//=\u003e Item { priority: 8, value: 'E' },\n\nmaxQueue;\n//=\u003e Queue { queue: [\n// Item { priority: 8, value: 'E' },\n// Item { priority: 7, value: 'D' },\n// Item { priority: 5, value: 'C' },\n// Item { priority: 2, value: 'F' } ] }\n\nmaxQueue.values();\n//=\u003e [ 'E', 'D', 'C', 'F' ]\n\nmaxQueue.toPairs();\n//=\u003e [ [ 8, 'E' ], [ 7, 'D' ], [ 5, 'C' ], [ 2, 'F' ] ]\n```\n\n## API\n\nThe following documentation holds for both min \u0026 max priority queues. The below described `queue` instance is used to depict the same methods that are available to both a min and a max priority queue, without overlooking their above described differences and unique qualities.\n\n#### queue.`size`\n\n- Return Type: `Number`\n\nReturns the total number of items residing in the queue.\n\n```js\nqueue.enqueue(15, 'A').enqueue(10, 'B').enqueue(5, 'C');\nqueue.size;\n// =\u003e 3\n```\n\n#### queue.`clear()`\n\n- Return Type: `Queue`\n\nMutates the queue by removing all residing items and returns it empty.\n\n```js\nqueue.enqueue(15, 'A').enqueue(10, 'B').enqueue(5, 'C');\n//=\u003e Queue { queue: [\n// Item { priority: 15, value: 'A' },\n// Item { priority: 10, value: 'B' },\n// Item { priority: 5, value: 'C' } ] }\nqueue.size;\n//=\u003e 3\nqueue.clear();\n//=\u003e Queue { queue: [] } }\nqueue.size;\n//=\u003e 0\n```\n\n#### queue.`dequeue()`\n\n- Return Type: `Item | undefined`\n\nMutates the queue by removing the front/highest-priority item which is also returned. If the queue is empty then `undefined` is returned.\n\n```js\nqueue.enqueue(15, 'A').enqueue(10, 'B').enqueue(5, 'C').enqueue(8, 'D');\nqueue.dequeue();\n// =\u003e Item { priority: 15, value: 'A' }\nqueue.dequeue();\n// =\u003e Item { priority: 10, value: 'B' }\nqueue.dequeue();\n// =\u003e Item { priority: 8, value: 'D' }\nqueue.dequeue();\n// =\u003e Item { priority: 5, value: 'C' }\nqueue.dequeue();\n// =\u003e undefined\nqueue.size;\n//=\u003e 0\n```\n\n#### queue.`enqueue(priority, value)`\n\n- Return Type: `Queue`\n\nMutates the queue by inserting a new item and returns the queue itself.\n\n##### **`priority`**\n\n- Type: `Number`\n\nCan be any number that will correspond to the `priority` of the created item. \n\n##### **`value`**\n\n- Type: `Any`\n\nCan be any value that will stored in the new item.\n\n```js\nqueue.enqueue(15, 'A');\n//=\u003e Queue { queue: [ Item { priority: 15, value: 'A' } ] }\nqueue.enqueue(10, 'B').enqueue(5, 'C');\n//=\u003e Queue { queue: [ \n// Item { priority: 15, value: 'A' },\n// Item { priority: 10, value: 'B' },\n// Item { priority: 5, value: 'C' } ] }\nqueue.size;\n//=\u003e 3\n```\n\n#### queue.`forEach(fn)`\n\n- Return Type: `Queue`\n\nApplies level order traversal (breadth-first traversal) to the binary heap, used internally for implementing the queue, and executes the provided `fn` function on each traversed item without mutating the queue. Returns the queue itself at the end of the traversal.\n\n##### **`fn`**\n\n- Type: `Function`\n\nUnary function to execute on each item.\n\n```js\nqueue.enqueue(15, 'A').enqueue(10, 'B').enqueue(5, 'C').enqueue(8, 'D');\n//=\u003e Queue { queue: [\n// Item { priority: 15, value: 'A' },\n// Item { priority: 10, value: 'B' },\n// Item { priority: 5, value: 'C' },}\n// Item { priority: 8, value: 'D' } } ]\nqueue.forEach(item =\u003e console.log(item.priority));\n//=\u003e 15\n//=\u003e 10\n//=\u003e 5\n//=\u003e 8\n```\n\n#### queue.`includes(value)`\n\n- Return Type: `Boolean`\n\nDetermines whether the queue includes a item with a certain `value`, returning `true` or `false` as appropriate.\n\n##### **`value`**\n\n- Type: `Any`\n\nItem `value` to search for.\n\n```js\nqueue.enqueue(15, 'A').enqueue(10, 'B').enqueue(5, 'C');\nqueue.includes('A');\n// =\u003e true\nqueue.includes('D');\n// =\u003e false\nqueue.includes('C');\n// =\u003e true\n```\n\n#### queue.`isEmpty()`\n\n- Return Type: `Boolean`\n\nDetermines whether the queue is empty, returning `true` or `false` as appropriate.\n\n```js\nqueue.enqueue(10, 'A');\nqueue.isEmpty();\n//=\u003e false\nqueue.clear().isEmpty();\n//=\u003e true\n```\n\n#### queue.`peek()`\n\n- Return Type: `Item | undefined`\n\nReturns the front/highest-priority item of the queue without removing it.\nIf the queue is empty `undefined` is returned.\n\n```js\nqueue.enqueue(10, 'A');\nqueue.peek();\n// =\u003e Item { priority: 10, value: 'A' }\n```\n\n#### queue.`peekPriority()`\n\n- Return Type: `Number | undefined`\n\nReturns the priority of the front/highest-priority item of the queue.\nIf the queue is empty `undefined` is returned.\n\n```js\nqueue.enqueue(10, 'A');\nqueue.peekPriority();\n// =\u003e 10\n```\n\n#### queue.`peekValue()`\n\n- Return Type: `Any | undefined`\n\nReturns the value of the front/highest-priority item of the queue.\nIf the queue is empty `undefined` is returned.\n\n```js\nqueue.enqueue(10, 'A');\nqueue.peekValue();\n// =\u003e 'A'\n```\n\n#### queue.`priorities()`\n\n- Return Type: `Array\u003cNumber\u003e`\n\nApplies level order traversal (breadth-first traversal) to the binary heap, used internally for implementing the queue, and stores the `priority` of each traversed item in an array.\nThe array is returned at the end of the traversal.\n\n```js\nqueue.enqueue(15, 'A').enqueue(10, 'B').enqueue(5, 'C').enqueue(8, 'D');\n//=\u003e [ 15, 10, 5, 8 ]\n```\n\n#### queue.`search(value)`\n\n- Return Type: `Item | undefined`\n\nDetermines whether the queue includes an item with a certain `value`, returning the targeted item or `undefined` as appropriate.\n\n##### **`value`**\n\n- Type: `Any`\n\nItem `value` to search for.\n\n```js\nqueue.enqueue(15, 'A').enqueue(10, 'B').enqueue(5, 'C').enqueue(8, 'D');\nqueue.search(10);\n// =\u003e Item { priority: 10, value: 'B' }\nqueue.search(5);\n// =\u003e Item { priority: 5, value: 'C' }\nqueue.search(25);\n// =\u003e undefined\n```\n\n#### queue.`toArray()`\n\n- Return Type: `Array\u003cItem\u003e`\n\nApplies level order traversal (breadth-first traversal) to the binary heap, used internally for implementing the queue, and stores each traversed item in an array.\nThe array is returned at the end of the traversal.\n\n```js\nqueue.enqueue(15, 'A').enqueue(10, 'B').enqueue(5, 'C').enqueue(8, 'D');\nqueue.toArray();\n//=\u003e [ \n//  Item { priority: 15, value: 'A' },\n//  Item { priority: 10, value: 'B' },\n//  Item { priority: 5, value: 'C' },\n//  Item { priority: 8, value: 'D' }\n// ]\n```\n\n#### queue.`toPairs()`\n\n- Return Type: `Array\u003c[Number, Any]\u003e`\n\nApplies level order traversal (breadth-first traversal) to the binary heap, used internally for implementing the queue, and for each traversed item stores in an array an ordered-pair/2-tuple, where the first element is a `number` corresponding to the `priority` of the traversed item, and the last one is a value of type `any`, corresponding to the `value` stored in the traversed item.\nThe array is returned at the end of the traversal.\n\n```js\nqueue.enqueue(15, 'A').enqueue(10, 'B').enqueue(5, 'C').enqueue(8, 'D');\nqueue.toPairs();\n//=\u003e [ [ 15, 'A' ], [ 10, 'B' ], [ 5, 'C' ], [ 8, 'D' ] ]\n```\n\n#### queue.`value()`\n\n- Return Type: `Array\u003cAny\u003e`\n\nApplies level order traversal (breadth-first traversal) to the binary heap, used internally for implementing the queue, and stores the `value` of each traversed item in an array.\nThe array is returned at the end of the traversal.\n\n```js\nqueue.enqueue(15, 'A').enqueue(10, 'B').enqueue(5, 'C').enqueue(8, 'D');\n//=\u003e [ 'A', 'B', 'C', 'D' ]\n```\n\nAlso available, along with the `Queue` exposed class, is the `Item` class, mainly useful for testing purposes, since it can be utilized to compare queue items. The class has a binary constructor method, with a `priority` and a `value` parameter, corresponding to the priority and the value stored in the created instance, respectively.\n\n#### item.`priority`\n\n- Return Type: `Number`\n\nThe `priority` corresponding to the item instance.\n\n```js\nconst {Item} = require('prioqueue');\n\nconst item = new Item(10, 'A');\n// =\u003e Item { priority:10, value: 'A' }\nitem.priority;\n//=\u003e 10\n```\n\n#### item.`value`\n\n- Return Type: `Any`\n\nThe value that the item contains.\n\n```js\nconst {Item} = require('prioqueue');\n\nconst item = new Item(10, 'A');\n// =\u003e Item { priority: 10, value: 'A' }\nitem.value;\n//=\u003e 'A'\nitem.value = 'B'\n// =\u003e Item { priority: 10, value: 'B' }\n```\n\n#### item.`toPair()`\n\n- Return Type: `[Number, Any]`\n\nReturns an ordered-pair/2-tuple, where the first element is a number corresponding to the `priority` of the item, and the last one is a value, that can be of any type, corresponding to the `value` stored in the item.\n\n```js\nconst {Item} = require('prioqueue');\n\nconst item = new Item(5, 'B');\n\nitem.toPair();\n//=\u003e [ 5, 'B' ]\n```\n\n## Development\n\nFor more info on how to contribute to the project, please read the [contributing guidelines](https://github.com/klaussinani/prioqueue/blob/master/contributing.md).\n\n- Fork the repository and clone it to your machine\n- Navigate to your local fork: `cd prioqueue`\n- Install the project dependencies: `npm install` or `yarn install`\n- Lint the code and run the tests: `npm test` or `yarn test`\n\n## Related\n\n- [avlbinstree](https://github.com/klaussinani/avlbinstree) - AVL self-balancing binary search trees for ES6\n- [binoheap](https://github.com/klaussinani/binoheap) - Binomial heaps for ES6\n- [binstree](https://github.com/klaussinani/binstree) - Binary search trees for ES6\n- [doublie](https://github.com/klaussinani/doublie) - Doubly circular \u0026 linear linked lists for ES6\n- [dsforest](https://github.com/klaussinani/dsforest) - Disjoint-set forests for ES6\n- [kiu](https://github.com/klaussinani/kiu) - FIFO Queues for ES6\n- [mheap](https://github.com/klaussinani/mheap) - Binary min \u0026 max heaps for ES6\n- [shtack](https://github.com/klaussinani/shtack) - LIFO Stacks for ES6\n- [singlie](https://github.com/klaussinani/singlie) - Singly circular \u0026 linear linked lists for ES6\n\n## Team\n\n- Klaus Sinani [(@klaussinani)](https://github.com/klaussinani)\n\n## License\n\n[MIT](https://github.com/klaussinani/prioqueue/blob/master/license.md)\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklaudiosinani%2Fprioqueue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fklaudiosinani%2Fprioqueue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklaudiosinani%2Fprioqueue/lists"}