{"name":"fastpriorityqueue","description":"Fast heap-based priority queue in JavaScript","keywords":["heap","binary heap","data structure","priority queue","performance"],"version":"0.8.0","repository":{"type":"git","url":"git+https://github.com/lemire/FastPriorityQueue.js.git"},"main":"FastPriorityQueue.js","types":"FastPriorityQueue.d.ts","scripts":{"test":"mocha unit/basictests.js","benchmark":"node benchmark/test.js"},"license":"Apache-2.0","bugs":{"url":"https://github.com/lemire/FastPriorityQueue.js/issues"},"homepage":"https://github.com/lemire/FastPriorityQueue.js#readme","devDependencies":{"benchmark":"^2.1.4","binaryheapx":"^0.1.1","heap":"^0.2.7","js-heap":"^0.3.1","js-priority-queue":"^0.1.5","mocha":"^11.3.0","priority_queue":"^0.1.3","priorityqueuejs":"^2.0.0","qheap":"^1.4.0","queue-priority":"^1.0.0","stablepriorityqueue":"^0.1.2","yabh":"^1.2.0"},"author":{"name":"Daniel Lemire","email":"lemire@gmail.com","url":"http://lemire.me/en/"},"gitHead":"01f72268e7b348100a491f71e6ff548e3395527d","_id":"fastpriorityqueue@0.8.0","_nodeVersion":"25.5.0","_npmVersion":"11.8.0","dist":{"integrity":"sha512-Nli86GbwvbI15pK0wQJSfrh+kMDQI9tae1QFO1HHgUdABL+WK/5rsFv0m/NfwHQmxTPRDhJTgHKRwfstJMxoSA==","shasum":"58efba8982d4b060e5f7887faf9b28ba50d1baba","tarball":"http://registry-npm.ywwl.com/fastpriorityqueue/download/fastpriorityqueue-0.8.0.tgz","fileCount":6,"unpackedSize":31473,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIHeXwIORXIl1IoBpu59DXseFW2ZZKi63E99PtcCt62WFAiEA7nwLHHLIkBTmb6oGyl3pp2Z05/RsVmfi2BGnkrNxW+8="}],"size":9920,"noattachment":false,"key":"/fastpriorityqueue/-/fastpriorityqueue-0.8.0.tgz"},"_npmUser":{"name":"lemire","email":"lemire@gmail.com"},"directories":{},"maintainers":[{"name":"lemire","email":"lemire@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/fastpriorityqueue_0.8.0_1771996710192_0.41830634331505845"},"_hasShrinkwrap":false,"readme":"# FastPriorityQueue.js : a fast, heap-based priority queue in JavaScript\n\n\nIn a priority queue, you can...\n\n* query or remove (poll) the smallest element quickly\n* insert elements quickly\n\nIn practice, \"quickly\" often means in logarithmic time (O(log n)).\n\nA heap can be used to implement a priority queue.\n\nFastPriorityQueue is an attempt to implement a performance-oriented priority queue\nin JavaScript. It can be several times faster than other similar libraries.\nIt is ideal when performance matters.\n\nLicense: Apache License 2.0\n\n# Usage\n\n```javascript\nvar x = new FastPriorityQueue();\nx.add(1);\nx.add(0);\nx.add(5);\nx.add(4);\nx.add(3);\nx.peek(); // should return 0, leaves x unchanged\nx.size; // should return 5, leaves x unchanged\nwhile (!x.isEmpty()) {\n  console.log(x.poll());\n} // will print 0 1 3 4 5\nx.trim(); // (optional) optimizes memory usage\n```\n\nYou can also provide the constructor with a comparator function.\n\n```javascript\nvar x = new FastPriorityQueue(function(a, b) {\n  return a > b;\n});\nx.add(1);\nx.add(0);\nx.add(5);\nx.add(4);\nx.add(3);\nwhile (!x.isEmpty()) {\n  console.log(x.poll());\n} // will print 5 4 3 1 0\n```\n\nIf you are using node.js, you need to import the module:\n\n```javascript\nvar FastPriorityQueue = require('fastpriorityqueue');\nvar b = new FastPriorityQueue(); // initially empty\nb.add(1); // add the value \"1\"\n```\n\nInstance methods summary:\n\n* `add(value)`: add an element into the queue; runs in `O(log n)` time.\n* `poll()`: remove and return the element on top of the heap (smallest element); runs in `O(log n)` time. If the priority queue is empty, the function returns `undefined`.\n* `remove(value)`: remove an element matching the provided value, if found, from the queue. The item is matched by using the queue's comparator. Returns `true` if the element is removed, `false` otherwise.\n* `removeOne(callback)`: execute the callback function for each item of the queue and remove the first item for which the callback will return true. Returns the removed item, or `undefined` if nothing is removed. The callback must be a pure function.\n* `removeMany(callback[, limit])`: execute the callback function for each item of the queue and remove each item for which the callback will return true, up to a max limit of removed items if specified or no limit if unspecified. Returns an array containing the removed items. The callback must be a pure function.\n* `replaceTop(value)`: `poll()` and `add(value)` in one operation. This is useful for [fast, top-k queries](http://lemire.me/blog/2017/06/21/top-speed-for-top-k-queries/). Returns the removed element or `undefined`, similar to `poll()`.\n* `heapify(array)`: replace the content of the heap with the provided array, then order it based on the comparator.\n* `peek()`: return the top of the queue (smallest element) without removal, or `undefined` if the queue is empty; runs in `O(1)` time.\n* `isEmpty()`: return `true` if the the queue has no elements, false otherwise.\n* `clone()`: copy the priority queue into another, and return it. Queue items are shallow-copied. Runs in `O(n)` time.\n* `forEach(callback)`: iterate over all items in the priority queue from smallest to largest. `callback` should be a function that accepts two arguments, `value` (the item), and `index`, the zero-based index of the item.\n* `trim()`: clean up unused memory in the heap; useful after high-churn operations like many `add()`s then `remove()`s.\n\n# npm install\n\n      $ npm install fastpriorityqueue\n\n# Computational complexity\n\nThe function calls \"add\" and \"poll\" have logarithmic complexity with respect\nto the size of the data structure (attribute size). Looking at the top value\nis a constant time operation.\n\n# Testing\n\nUsing node.js (npm), you can test the code as follows...\n\n      $ npm install mocha\n      $ npm test\n\n# Is it faster?\n\nIt tends to fare well against the competition.\nIn some tests, it can be five times faster than any other\nJavaScript implementation we could find.\n\n```\n$ node benchmark/test.js\nPlatform: darwin 20.1.0 x64\nIntel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\nNode version 14.7.0, v8 version 8.4.371.19-node.12\n\nComparing against:\njs-priority-queue: https://github.com/adamhooper/js-priority-queue 0.1.5\nstablepriorityqueue: https://github.com/lemire/StablePriorityQueue.js 0.1.2\nheap.js: https://github.com/qiao/heap.js 0.2.6\nbinaryheapx: https://github.com/xudafeng/BinaryHeap 0.1.1\npriority_queue: https://github.com/agnat/js_priority_queue 0.1.3\njs-heap: https://github.com/thauburger/js-heap 0.3.1\nqueue-priority: https://github.com/augustohp/Priority-Queue-NodeJS 1.0.0\npriorityqueuejs: https://github.com/janogonzalez/priorityqueuejs 2.0.0\nqheap: https://github.com/andrasq/node-qheap 1.4.0\nyabh: https://github.com/jmdobry/yabh 1.2.0\n\nstarting dynamic queue/enqueue benchmark\nFastPriorityQueue x 36,816 ops/sec ±0.74% (92 runs sampled)\nFastPriorityQueue---replaceTop x 107,942 ops/sec ±0.71% (91 runs sampled)\nsort x 6,240 ops/sec ±1.65% (92 runs sampled)\nStablePriorityQueue x 10,333 ops/sec ±4.09% (91 runs sampled)\njs-priority-queue x 14,435 ops/sec ±1.97% (91 runs sampled)\nheap.js x 6,568 ops/sec ±2.29% (90 runs sampled)\nbinaryheapx x 8,595 ops/sec ±0.56% (94 runs sampled)\npriority_queue x 8,201 ops/sec ±0.74% (94 runs sampled)\njs-heap x 557 ops/sec ±1.70% (89 runs sampled)\nqueue-priority x 291 ops/sec ±2.46% (88 runs sampled)\npriorityqueuejs x 13,864 ops/sec ±2.02% (90 runs sampled)\nqheap x 26,882 ops/sec ±1.81% (93 runs sampled)\nyabh x 10,472 ops/sec ±1.50% (93 runs sampled)\nFastest is FastPriorityQueue\n```\n\nBenchmarks on an Apple M1:\n```\nPlatform: darwin 20.2.0 arm64\nApple M1\nNode version 15.6.0, v8 version 8.6.395.17-node.23\n\nComparing against:\njs-priority-queue: https://github.com/adamhooper/js-priority-queue 0.1.5\nstablepriorityqueue: https://github.com/lemire/StablePriorityQueue.js 0.1.2\nheap.js: https://github.com/qiao/heap.js 0.2.6\nbinaryheapx: https://github.com/xudafeng/BinaryHeap 0.1.1\npriority_queue: https://github.com/agnat/js_priority_queue 0.1.3\njs-heap: https://github.com/thauburger/js-heap 0.3.1\nqueue-priority: https://github.com/augustohp/Priority-Queue-NodeJS 1.0.0\npriorityqueuejs: https://github.com/janogonzalez/priorityqueuejs 2.0.0\nqheap: https://github.com/andrasq/node-qheap 1.4.0\nyabh: https://github.com/jmdobry/yabh 1.2.0\n\nstarting dynamic queue/enqueue benchmark\nFastPriorityQueue x 47,894 ops/sec ±0.19% (100 runs sampled)\nFastPriorityQueue---replaceTop x 187,809 ops/sec ±0.09% (97 runs sampled)\nsort x 9,285 ops/sec ±0.10% (100 runs sampled)\nStablePriorityQueue x 19,830 ops/sec ±0.49% (97 runs sampled)\njs-priority-queue x 28,382 ops/sec ±0.10% (98 runs sampled)\nheap.js x 5,504 ops/sec ±0.22% (100 runs sampled)\nbinaryheapx x 10,473 ops/sec ±0.11% (98 runs sampled)\npriority_queue x 9,041 ops/sec ±0.33% (97 runs sampled)\njs-heap x 390 ops/sec ±0.04% (96 runs sampled)\nqueue-priority x 438 ops/sec ±0.09% (95 runs sampled)\npriorityqueuejs x 14,797 ops/sec ±0.07% (101 runs sampled)\nqheap x 38,108 ops/sec ±0.12% (99 runs sampled)\nyabh x 14,942 ops/sec ±0.24% (99 runs sampled)\n```\n\nNote that `qheap` has been updated following the introduction of `FastPriorityQueue`, with a reference to `FastPriorityQueue` which might explains the fact that its performance is comparable to `FastPriorityQueue`.\n\n# Insertion order\n\nA binary heap does not keep track of the insertion order.\n\n# You might also like...\n\nIf you like this library, you might also like\n\n* https://github.com/lemire/FastBitSet.js\n* https://github.com/lemire/StablePriorityQueue.js\n* https://github.com/lemire/FastIntegerCompression.js\n","publish_time":1771996710342,"_cnpm_publish_time":1771996710342,"dist-tags":{"latest":"0.8.0"}}