Sub topic 5
Hidden Classes: Giving Dynamic Objects a Fixed Layout
This chapter assumes you can write JavaScript constructors, classes, and object literals, and that you can run a script with Node.js from the command line. It builds on the earlier chapters about the engine pipeline and the interpreter. Those chapters covered how code gets executed. This one covers how the objects that code works on are stored in memory.
The problem: objects without a declared layout
In C, Java, or Rust, the compiler knows every field of a struct or class before the program runs. A field access like point.x compiles to "load the value at offset 0 from this address", which is a single machine instruction.
JavaScript gives the engine no such promise. Properties can be added at any time, deleted, or created under computed names:
const obj = {};
obj.x = 1;
obj['y' + 1] = 2;
delete obj.x;The simplest correct way to store such an object is a hash table, called a dictionary, that maps property names to values. Every obj.x then means hashing the string "x", probing a table, and comparing keys. That is many times slower than a fixed-offset load. It also wastes memory, because every object carries its own copy of every key.
Real programs rarely use this flexibility. Most objects created at one place in the code end up with the same properties in the same order. Engines take advantage of that pattern with hidden classes.
What a hidden class is
A hidden class is an internal description of an object's layout. It records which properties the object has, in what order, and where each value is stored. Each engine uses its own name for it:
| Engine | Name for the hidden class | Used by |
|---|---|---|
| V8 | Map | Chrome, Node.js, Deno |
| SpiderMonkey | Shape | Firefox |
| JavaScriptCore | Structure | Safari, Bun |
The rest of this chapter uses V8's term, map. Don't confuse it with the JavaScript Map collection, which is unrelated.
Every V8 heap object starts with a pointer to its map. The object then holds only the values. The property names, their attributes (writable, enumerable, configurable), and their offsets live in the map, so all objects with the same layout share a single map.
What a map contains
A V8 map holds more than property names. Its main fields are:
- Instance type: what kind of object this is, such as a plain object, an array, or a function.
- Instance size and in-object property count: how many bytes the object takes up and how many property slots sit directly inside it.
- Prototype: the object's
[[Prototype]]. This is why two objects with identical properties but different prototypes always have different maps. - Descriptor array: the list of property names, their attributes, and their storage locations.
- Elements kind: how indexed elements are stored. A later chapter covers this.
- Transitions: links to the maps an object moves to when a property is added. The next chapter covers these.
Because the layout lives in the map, the engine can answer "where is x?" with one comparison: check whether the object's map is the expected map, and if it is, load the value from the known slot. Inline caches, covered in a later chapter, are built on exactly this check.
Where property values are stored
V8 has two places to put named property values:
- In-object properties are stored directly inside the object's memory. They are the fastest, because the slot is a fixed offset from the object's address.
- Out-of-object properties go into a separate array called the properties backing store, which the object points to. Access needs one extra pointer load. The store grows when you add more properties.
When V8 allocates an object, it decides how many in-object slots to reserve. For an object literal, it reserves slots for the properties written in the literal. For a constructor, V8 uses slack tracking: the first few objects get generous space, and once V8 has seen how many properties the constructor actually adds, it shrinks the instance size for later objects. Properties added after construction usually spill into the backing store.
class Point {
constructor(x, y) {
this.x = x; // in-object slot
this.y = y; // in-object slot
}
}
const p = new Point(1, 2);
p.label = 'origin'; // added later: may land in the out-of-object backing store
console.log(p.label);Seeing shared maps in Node.js
V8 provides internal functions that can only be called when the --allow-natives-syntax flag is set. %HaveSameMap(a, b) returns 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)); // false
const lit1 = { x: 1, y: 2 };
const lit2 = { x: 9, y: 8 };
console.log(%HaveSameMap(lit1, lit2)); // true: same keys, same order, same prototype
console.log(%HaveSameMap(a, lit1)); // false: different prototype and constructorThe % syntax is a parse error without the flag. Never use it in production code. %DebugPrint(obj) prints the map address, the property layout, and whether each property is in-object. Its exact output changes between V8 versions.
Values do not change the map, mostly
A map describes property names and their locations, not the values. Writing a.x = 100 keeps the map. V8 does track a representation for each field, such as small integer (Smi), double, heap object, or "any". If a field that has only ever held integers gets a string, V8 generalizes that field's representation in the map. This can invalidate optimized code that relied on the narrower representation. Use a property for one type of value throughout its life, and you avoid these field generalizations.
Common misconceptions
| Belief | What actually happens |
|---|---|
| Objects with the same keys share a map | Only if the keys were added in the same order with the same prototype |
| `class` syntax creates a different kind of object | Classes and constructor functions produce maps in the same way |
| Changing a value changes the hidden class | Values don't change it, though a type change can generalize a field's representation |
| Hidden classes are a V8 quirk | SpiderMonkey and JavaScriptCore use the same idea under other names |
| Every object gets its own map | Maps are shared, which is what makes them cheap |
Trade-offs of the design
Hidden classes work well when objects have stable, predictable layouts. They cost memory and time when layouts vary a lot. If you create objects with many different property combinations, V8 has to build many maps, and code that touches all of them gets slower. Objects used as hash maps with arbitrary keys are a poor fit. V8 handles them by switching to dictionary mode, which a later chapter covers. For that kind of data, the built-in Map collection is usually the better choice.
Exercise
Two objects are created as const m = { a: 1, b: 2 } and const n = Object.create(null); n.a = 1; n.b = 2;. Will %HaveSameMap(m, n) return true? Explain why.
Show answer
No. The map records the prototype. m has Object.prototype as its prototype and n has null, so they must have different maps even though they have the same keys in the same order. Also, V8 usually puts objects created with Object.create(null) in dictionary mode from the start.
Exercise
A constructor assigns this.id and this.name. Later, only some instances get this.cache = {}. What happens to those instances' maps and to where cache is stored, and how would you fix it?
Show answer
The instances that get cache move to a new map, so instances of this one constructor now have two maps. The cache value probably goes into the out-of-object backing store, because slack tracking has already sized instances for two properties. The fix is to initialize this.cache = null in the constructor so all instances share one map with three in-object slots.
Exercise
Why can V8 find a property's location with a single map comparison instead of a hash lookup?
Show answer
All objects with the same map share the same layout, so the map pointer stands for the property's offset. If an object's map equals the map seen before, the property is at the same slot, and the engine can load it directly from that fixed offset.