The smallest and simplest binary heap priority queue in JavaScript.
// create an empty priority queuevar queue = new TinyQueue();// add some itemsqueue.push(7);queue.push(5);queue.push(10);// remove the top itemvar top = queue.pop(); // returns 5// return the top item (without removal)top = queue.peek(); // returns 7// get queue lengthqueue.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 argumentqueue = new TinyQueue([{value: 5}, {value: 7}], function (a, b) { return a.value - b.value;});// turn a queue into a sorted arrayvar array = [];while (queue.length) array.push(queue.pop());