Understanding the Shape of JavaScript Code in V8 Memory: Stack, Heap, and Constant Pool
This article explains how JavaScript code is represented in V8's memory, covering primitive types, the roles of the stack, heap, and constant pool, as well as function prototypes, variable hoisting, and garbage collection, with clear examples and diagrams.
JavaScript has several primitive data types such as number , string , boolean , null and undefined , and many developers wonder about the results of typeof null and typeof undefined , the differences between var , let , const , and why === checks both value and type.
In a browser, the core consists of a rendering engine and a JavaScript engine. Modern browsers like Chrome use the V8 engine, which parses code into tokens, builds an Abstract Syntax Tree (AST), and applies various optimizations before execution.
V8’s memory can be divided into three main regions: the stack , the heap , and the constant pool . The stack stores variable names and their addresses, the heap holds objects, and the constant pool stores immutable values like strings, numbers and booleans.
Stack : The stack is a contiguous, small region where variable identifiers map to memory addresses. undefined occupies a special fixed slot in the stack. Example:
console.log(b); // undefined
var a;
var b = '政采云前端团队';During execution, variable hoisting creates entries for a and b on the stack, initially pointing to undefined . When the assignment runs, b ’s address is updated to point to the constant pool entry.
Constant Pool : This area stores unique immutable values. Because each constant is stored only once, two variables that reference the same literal share the same memory address, which explains why a === b can be true for identical strings.
var a = '政采云前端团队';
var b = '政采云前端团队';Heap : All objects reside in the heap, including arrays, functions, and the special null object. The heap’s layout is more complex, but conceptually each object occupies its own space.
typeof {}; // object
typeof []; // object
typeof null; // objectFunctions are also objects. When a function is defined, V8 creates a prototype object in memory and links it via the prototype property. The prototype chain is built from these objects.
function Animal(name) {
this.name = name;
}
Animal.prototype.eat = function() {
console.log('Animal eat');
};
function Dog(name) {
Animal.apply(this, arguments);
}
Dog.prototype = new Animal();
Dog.prototype.constructor = Dog;
var dog = new Dog();
dog.eat();When the new operator is used, V8 allocates a new object on the heap, sets its __proto__ to the constructor’s prototype , and returns the instance. This explains why Animal.prototype === animal.__proto__ evaluates to true .
Garbage collection (GC) reclaims heap memory that is no longer reachable. However, variables that still reference a function’s execution context can keep that memory alive, which is a common source of memory leaks in closures.
In summary, understanding V8’s stack, heap, constant pool, and prototype mechanisms provides a concrete mental model for how JavaScript code executes, how values are stored, and why certain language quirks behave the way they do.
政采云技术
ZCY Technology Team (Zero), based in Hangzhou, is a growth-oriented team passionate about technology and craftsmanship. With around 500 members, we are building comprehensive engineering, project management, and talent development systems. We are committed to innovation and creating a cloud service ecosystem for government and enterprise procurement. We look forward to your joining us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.