Priority Queue for Node.js
It's a simple implementation of a priority queue for Node.js. It uses binary search to keep items ordered as
they are inserted. By doing so, its performances are much more stable and attractive than the manual method of pushing
items into an array and using
How do you use it?
var PriorityQueue = require('pqueue'); var q = new PriorityQueue(opts); q.push(1,2,3) q.pop() // => 3 q.pop(2) // => [1,2]
What are the options?
comparator: A comparator
function(a,b)that returns -1 if
a<b, +1 if
a>bor 0 if ... you know
priority: A function that returns the rank of a given item, used as the basis for comparison. Only used if
comparatorwas not provided.
capacity: The maximum number of items the queue will contain. Lower priority items will be discarded, unless ...
reversed: If true, higher priority items will be discarded instead of lower ones (only used when
What are the methods?
q.add(item[, items*])(aliased as
Adds one or more items in the queue, keeping it ordered and discarding items if necessary.
nisn't given, it removes and returns one item from the top of the queue. If
nis given, remove and return an array of
nitems from the top of the queue.
nisn't given, it removes and returns one item from the bottom of the queue. If
nis given, well ... fill the blanks.
Empty the queue
What are its properties?
q.length: its length (read-only)
q.capacity: its capacity. If set, items will be dropped when the capacity is exceeded.
q.queue: its content (read-only)
q.bottomthe top and bottom items in the queue (also read-only)