Why can two objects with the same useful properties make one property read much slower? In V8, the answer may be their hidden classes. A hidden class—also called a shape or map—is an internal blueprint for an object’s property layout. Objects with the same structure can share one, giving the engine a predictable route to each property. JavaScript specifies how property access behaves, not that engines must implement hidden classes. section 5.10.1 section 5.10
An object begins with an initial shape. Adding, redefining, or deleting a property changes it. Consider `const a = { x: 1, y: 2 }; const b = { x: 1 }; b.y = 2;`: the objects end with the same property names, but their construction paths differ. Repeatedly creating objects with optional fields or varying assignment order can produce many shapes rather than one reliably shared shape. Constructor assignments also produce intermediate shapes, though engines can optimize that consistent construction pattern. section 5.10.1
A predictable layout
A factory that returns `{ label, contents, isOpen: false, accessCount: 0 }` for every call gives its results a consistent structure. Contrast a factory that conditionally adds `lockCode`, `motorSpeed`, and `sensorType`, then adds `priority` and `department` in either order. The latter creates several construction paths for objects used in the same role. When practical, establish the intended properties together and in a consistent order. section 5.10.1
function createProblematicDrawer(label, contents, config = {}) {
const drawer = { label, contents };
if (config.secure) { #A
drawer.lockCode = config.lockCode; #A
} #A
#A
if (config.automated) { #A
drawer.motorSpeed = config.motorSpeed; #A
drawer.sensorType = config.sensorType; #A
} #A
if (Math.random() > 0.5) { #B
drawer.priority = 'high'; #B
drawer.department = config.department; #B
} else { #B
drawer.department = config.department; #B
drawer.priority = 'high'; #B
} #B
return drawer;
}The conditional-property factory makes the sources of shape variation concrete: optional fields and changing insertion order. section 5.10.1
Hidden classes matter especially at a hot property-access site. An inline cache can remember where a property was found for a shape and reuse that information. A new shape can invalidate the remembered location and require another lookup. For a function reading `point.x`, V8 classifies a site seeing one shape as monomorphic, two to four as polymorphic, and more than four as megamorphic. At that last transition, it stops tracking shapes at the site and changes strategy. section 5.10.2
Table 5.2 20 million property loads per measurement, using Node.js 24.
|
Distinct Shapes
|
IC state
|
Milliseconds
|
Nanoseconds per load
|
Vs. Monomorphc
|
|
1
|
Monomorphic
|
22.31
|
1.116
|
1.00x
|
|
2
|
Polymorphic
|
31.73
|
1.587
|
1.42x
|
|
4
|
Polymorphic
|
41.88
|
2.094
|
1.88x
|
|
5
|
Megamorphic
|
108.29
|
5.414
|
4.85x
|
|
8
|
Megamorphic
|
101.89
|
5.094
|
4.57x
|
|
64
|
Megamorphic
|
103.21
|
5.160
|
4.63x
|
Node.js 24 measurements show the property-load cost rising sharply between four distinct shapes and five; these are measurements, not a JavaScript-language guarantee. section 5.10.2
A slowdown is a clue, not proof of a shape problem. V8’s `--log-ic` can expose inline-cache state changes in Node.js; a CPU profile can identify a simple function taking disproportionate time, and Node.js `--trace-deopt` can help investigate optimizer behavior. Such diagnostics are runtime-specific: another engine may optimize differently or lack the same flags. Keep object layouts predictable where profiling shows property access matters, rather than treating one engine’s thresholds as language rules. section 5.10.2 section 11.5

