Sub topic 5
Hidden Classes: Giving Dynamic Objects a Fixed Layout
This chapter assumes you can write JavaScript constructors and object literals, run scripts with Node.js, and remember that the interpreter executes bytecode. Earlier chapters covered how engines parse, interpret, and compile code. This chapter covers how they store objects.
The problem with dynamic objects
In JavaScript, you can add properties to an object or remove them at any time. A naive engine would store each object as a hash table that maps property names to values. That works, but every o.x would then need a hash lookup, which is slow compared with reading memory at a known offset.
Statically typed languages such as C++ avoid this cost. The compiler knows the layout of every struct, so p.x compiles to "load the value at offset 0 from p." Engines want the same speed without giving up JavaScript's flexibility.
The working model
A hidden class (called a map in V8 and a shape in SpiderMonkey and most papers) describes the layout of an object. It records:
- which property names the object has
- the slot, or offset, where each property's value is stored
- property attributes such as writable or enumerable
The object itself holds only a pointer to its hidden class and the property values. Many objects with the same layout point to one shared hidden class, so the per-property metadata is stored once instead of once per object.
function Point(x, y) {
this.x = x;
this.y = y;
}
const a = new Point(1, 2);
const b = new Point(3, 4);
// a and b were built the same way, so they share a hidden class.
const c = new Point(5, 6);
c.z = 7;
// Adding z moves c to a new hidden class; a and b are unchanged.Once the engine knows an object's hidden class, reading a.x becomes two steps: check the hidden class, then load slot 0. The engine never searches for the name x.
Checking it yourself
V8 exposes internal functions behind the --allow-natives-syntax flag. %HaveSameMap reports whether two objects share a map.
// Run with: node --allow-natives-syntax shapes.js
function Point(x, y) {
this.x = x;
this.y = y;
}
const a = new Point(1, 2);
const b = new Point(3, 4);
console.log(%HaveSameMap(a, b)); // true
b.z = 5;
console.log(%HaveSameMap(a, b)); // falseCommon misconceptions
| Misconception | Reality |
|---|---|
| Hidden classes are related to the `class` keyword | They are an engine-internal detail. Plain object literals get them too. |
| Same property names means same hidden class | Insertion order also matters, as the next chapter shows. |
| Values affect the hidden class | Mostly they don't. `a` and `b` hold different numbers but share a map. V8 does track field representations (such as small integer versus double), which can cause rare map changes. |
| Each object has its own hidden class | Objects with the same layout share one, which saves memory. |
The fixed layout matters most when combined with inline caches, which remember the hidden class seen at a property access. Later chapters build on that.
Exercise
Two objects are created with new Point(1, 2) and new Point(10, 20). Afterward, the second gets p2.y = "twenty". Do they still share a hidden class, and why does sharing a hidden class make p.x fast?
Show answer
In most cases no. V8 records a representation for each field, so storing a string where it has seen only small integers usually generalizes the field and gives p2 a new or deprecated map (p1 migrates later). Changing a value to another value of the same kind would not affect the map. Sharing a hidden class is fast because the engine checks one map pointer and then loads the value from a known slot offset instead of looking up the property name.
Exercise
Why do engines store property names and offsets in a shared hidden class instead of inside each object?
Show answer
Many objects have the same layout, so keeping the metadata once saves memory. It also gives the engine one identity to compare (a pointer to the hidden class), which makes fast checks and caching possible.