Heap
A binary heap is a complete binary tree stored in an array so the smallest (or largest) value is always at index 0. Push and pop are O(log n); peek is O(1).
Detailed Explanation
JavaScript has no Heap or PriorityQueue in the standard library. When you need "always take the next most important item" — Dijkstra, merge-k-lists, scheduling, top-k — you implement a binary heap or take one from a library.
A min-heap keeps a[parent] <= a[child]. A max-heap flips the comparison. The tree is complete: every level is filled left to right. That shape maps to an array. For index i, parent is ((i - 1) >> 1), left child is 2 * i + 1, right child is 2 * i + 2. No pointers.
push appends at the end (the next open leaf) and sifts up until the parent order is restored. pop takes index 0, moves the last element into 0, and sifts down. Both walk at most the height of the tree, which is floor(log2 n). peek reads a[0] without removing it.
Building a heap from n items is O(n) if you sift down from the last parent, or O(n log n) if you push one by one. Sorting with a heap is O(n log n) and is how heap sort works; array.sort is the practical sort.
Comparison must be consistent. If you store objects, compare a key (task.dueAt, node.dist). Equality of keys is allowed; heaps are not stable. If you need to decrease a key (Dijkstra), either support an index map and sift from the middle, or insert a new node and ignore stale ones when they pop.
Do not confuse this with the memory heap. "Stack vs heap" is allocation. This heap is a data structure.
What it costs you: no O(1) find-by-value, no sorted iteration except by repeated pop (which empties it). A balanced BST (Map is not one; it is a hash table) can do ordered scans. A heap cannot. Also, a buggy comparator ((a, b) => a - b on objects) silently ruins the shape.
Reach for a heap when you repeatedly need the current min or max among a changing set. Reach for sort when the set is fixed and you want all of it ordered once. Reach for a Map when you look up by id.
Code Examples
function createMinHeap() {
const a = [];
function less(i, j) {
return a[i] < a[j];
}
function swap(i, j) {
const t = a[i];
a[i] = a[j];
a[j] = t;
}
function siftUp(i) {
while (i > 0) {
const p = (i - 1) >> 1;
if (!less(i, p)) break;
swap(i, p);
i = p;
}
}
function siftDown(i) {
for (;;) {
let s = i;
const l = i * 2 + 1;
const r = l + 1;
if (l < a.length && less(l, s)) s = l;
if (r < a.length && less(r, s)) s = r;
if (s === i) break;
swap(i, s);
i = s;
}
}
return {
push(x) {
a.push(x);
siftUp(a.length - 1);
},
pop() {
if (a.length === 0) return undefined;
const top = a[0];
const last = a.pop();
if (a.length) {
a[0] = last;
siftDown(0);
}
return top;
},
peek() {
return a[0];
},
get size() {
return a.length;
},
};
}
const h = createMinHeap();
h.push(5);
h.push(1);
h.push(4);
console.log(h.pop(), h.pop(), h.pop()); // => 1 4 5Common Gotchas
JavaScript has no stdlib heap
interview code is expected to implement one, or to sort when n is small enough that O(n log n) once is cheaper than talking about heaps.
Stale entries after a key change
if you cannot sift a middle index, push a new pair and skip old pops when a `visited` or version says they are outdated.
Peek does not remove
a loop that only `peek`s never drains. Use `pop`.
This is not the GC heap
"allocated on the heap" is a different meaning of the word.