Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is heap in JavaScript

Welcome to the World of JavaScript

You've been learning JavaScript, and you've probably heard about a "Heap". No, we're not talking about a pile of objects, but a term used in the world of programming. Don't worry if it seems complex at first, we're going to break it down piece by piece, so it's easy to understand.

Heap? You Mean Like a Heap of Clothes?

Well, not quite, but you're on the right track. Think of the heap as a big, messy pile where JavaScript stores most of your variables and function objects. It's a lot like a heap of clothes in your room. Just like you might toss a shirt or pants onto the heap without worrying too much about where they land, JavaScript throws objects onto the heap wherever it finds space.

let heapExample = {
    name: "Heap",
    description: "A place in memory where JavaScript stores objects."
};

In the example above, the object heapExample is stored in the heap.

Heap Vs Stack

In JavaScript, we have two primary places to store data: the heap and the stack. The stack is neat and organized, but it has limited space. It's like a small drawer where you keep your most essential items. On the other hand, the heap is vast and can store much more data, but it's not as organized.

function stackExample() {
    let a = 1; // This goes on the stack
    let b = 2; // This also goes on the stack
    // The heap comes into play when we start dealing with objects
    let heapObject = { a: a, b: b }; // This goes in the heap
}

In the example above, a and b are primitives and stored on the stack. However, heapObject is an object and gets stored in the heap.

Why Do We Need a Heap?

You might be wondering, "If the stack is so neat and organized, why don't we use that all the time?" The answer lies in the size limitation of the stack. The heap, though messier, allows us to work with larger and more complex structures without worrying about running out of space.

Imagine trying to fit a large, fluffy winter coat into a small drawer (the stack). It's not going to work, right? But you could easily toss it onto a heap of clothes (the heap) in your room.

Memory Management in the Heap

While the heap is large, it's not infinite. So, JavaScript has to manage the heap's memory to ensure we don't run out of space. This is where garbage collection comes in.

Garbage collection is like a helpful robot that cleans up your heap of clothes. If you haven't worn a piece of clothing in a while (if an object isn't being used anymore), the robot picks it up and takes it away to free up space.

function garbageCollectionExample() {
    let heapObject = { a:1, b:2 }; // This object is now in the heap
    // some code
    heapObject = null; // The garbage collector will eventually remove the original heapObject from the heap
}

In the above example, once we set heapObject to null, the original object that heapObject was referencing is no longer reachable. At this point, the garbage collector can remove it from the heap to free up space.

Conclusion: The Heap, A JavaScript Essential

Think of JavaScript as a busy tailor, deftly crafting objects and variables to make a program work. In this scenario, the heap is like a large fabric storeroom. It might be a bit messy, but it's a crucial part of the process, holding the materials needed for the tailor to work his magic. Understanding how the heap works and how JavaScript manages it is a significant step in your journey as a JavaScript artisan. Happy coding!