Min Heap Implementation in JavaScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Min Heap runs in O(log n) time and O(n) space, in JavaScript as in every other language.
- ✓Nothing. JavaScript has no heap and no priority queue in the standard library, so every implementation is hand-written with an array and two sift loops.
- ✓Store the heap in a plain array, take a comparator in the constructor so the same class serves as a min-heap or a max-heap, and use bitwise arithmetic for the index math ((i - 1) >> 1 for the parent), it is both idiomatic and faster than Math.floor.
- ✓The JavaScript trap: The workaround people reach for instead of writing the heap is calling array.sort() after every push, which turns an O(log n) insert into O(n log n) and is the single most common source of slow JavaScript pathfinding.
- ✓A flat numeric array is backed by a contiguous packed elements store in V8, so sift operations are cache-friendly and genuinely fast.
What Min Heap actually is
Min Heap in JavaScript: what you actually reach for
JavaScript implementation
class MinHeap {
#items = [];
#compare;
constructor(compare = (a, b) => a - b) {
this.#compare = compare; // never rely on default sort ordering
}
get size() { return this.#items.length; }
push(value) {
const items = this.#items;
items.push(value);
let i = items.length - 1;
while (i > 0) {
const parent = (i - 1) >> 1;
if (this.#compare(items[i], items[parent]) >= 0) break;
[items[i], items[parent]] = [items[parent], items[i]];
i = parent;
}
}
pop() {
const items = this.#items;
if (items.length === 0) return undefined;
const top = items[0];
const last = items.pop();
if (items.length > 0) {
items[0] = last;
let i = 0;
for (;;) {
const l = 2 * i + 1, r = l + 1;
let smallest = i;
if (l < items.length && this.#compare(items[l], items[smallest]) < 0) smallest = l;
if (r < items.length && this.#compare(items[r], items[smallest]) < 0) smallest = r;
if (smallest === i) break;
[items[i], items[smallest]] = [items[smallest], items[i]];
i = smallest;
}
}
return top;
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe JavaScript-specific trap
Performance in JavaScript, beyond the O(log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which JavaScript type or library should I use for Min Heap?
Nothing. JavaScript has no heap and no priority queue in the standard library, so every implementation is hand-written with an array and two sift loops. That absence is why so much JavaScript graph code is accidentally quadratic.
What is the most common JavaScript mistake when implementing Min Heap?
The workaround people reach for instead of writing the heap is calling array.sort() after every push, which turns an O(log n) insert into O(n log n) and is the single most common source of slow JavaScript pathfinding. Worse, Array.prototype.sort with NO comparator sorts lexicographically by string, so [10, 9, 1] sorts to [1, 10, 9], a numeric priority queue built that way is silently, subtly wrong.
How does Min Heap actually perform in JavaScript?
A flat numeric array is backed by a contiguous packed elements store in V8, so sift operations are cache-friendly and genuinely fast. Storing objects instead of numbers makes the array pointer-dense and roughly halves throughput. Avoid growing the heap with push beyond its capacity in tight loops, pre-sizing helps when the maximum size is known. Time complexity is O(log n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for Replit, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.