Sub topic 6
Shape Transitions and Why Property Order Matters
This chapter assumes you know what a hidden class (V8 calls it a map) is from the previous chapter. You should also be able to run Node.js with command-line flags. Here you will see how V8 builds hidden classes step by step, and why two objects with the same keys can end up with different layouts.
Transitions Form a Tree
V8 does not design a hidden class for an object all at once. Every object starts from a root map with no properties. Each time a property is added, V8 follows a transition, an edge from the current map to a new map that has one more property. If that edge already exists because an earlier object took the same path, V8 reuses the target map. If it doesn't exist, V8 creates a new map and records the edge.
Over time these edges form a transition tree. Objects built the same way walk the same path and end on the same map. Objects built differently branch off.
Each map stores the offset of every property it knows about. Map2 says "x is at slot 0, y is at slot 1". Map5 says the opposite. Code that has learned Map2's layout can't use it to read from an object with Map5.
Order Changes the Shape
You can confirm this with V8's native syntax:
// Run with: node --allow-natives-syntax order.js
const p = { x: 1, y: 2 };
const q = { y: 2, x: 1 };
console.log(%HaveSameMap(p, q)); // false: different insertion order
const r = { x: 3, y: 4 };
console.log(%HaveSameMap(p, r)); // true: same order, same mapThe values don't matter. Only the property names and the order they were added in decide the map.
The Common Mistake: Conditional Properties
Literals with keys written in a different order are rare in real code. More often, shapes split because a property is only added on some paths:
// Run with: node --allow-natives-syntax conditional.js
function makeUser(name, isAdmin) {
const u = {};
if (isAdmin) u.role = 'admin';
u.name = name;
return u;
}
const alice = makeUser('Alice', true);
const bob = makeUser('Bob', false);
console.log(%HaveSameMap(alice, bob)); // false
function makeUserStable(name, isAdmin) {
return { role: isAdmin ? 'admin' : null, name };
}
const carol = makeUserStable('Carol', true);
const dave = makeUserStable('Dave', false);
console.log(%HaveSameMap(carol, dave)); // trueThe stable version always creates every property, in the same order, and uses null when there's no value. Classes and constructors work the same way. If you assign every field in the constructor and don't add any later, all instances share one map.
Why It Matters
A few extra maps cost little memory. The real cost shows up at property-access sites. A site that sees one map can use a single fast offset lookup. A site that sees many maps gets slower, as the inline cache chapters will show. Removing properties is a separate and worse problem, covered in the next chapter on dictionary mode.
Exercise
A constructor runs this.a = 1; this.b = 2;. Afterwards, some instances get obj.c = 3 and others get obj.d = 4. How many distinct maps are created along the transition path starting from the root, and how would you make all instances share one map?
Show answer
The path is root, then {a}, then {a,b}, which then branches into {a,b,c} and {a,b,d}. That gives four maps beyond the root (five counting the root). To make all instances share one map, initialize both fields in the constructor, for example this.c = undefined; this.d = undefined;, so every instance ends on {a,b,c,d}.
Exercise
Do { x: 1, y: 2 } and { x: 'a', y: 'b' } share a hidden class?
Show answer
Yes, as far as property layout goes. Both have the same keys added in the same order. The value types don't change the transition path, although V8 may track field representations separately.