liveBook · Manning

Inline cache

As explained in JavaScript in Depth

A runtime optimization mechanism that caches information about previous property accesses or method calls to speed future lookups.

A property access can become slower without changing its source code. V8 speeds repeated lookups with an inline cache: it remembers where a property resides for a particular hidden class, then reuses that location when the next object has the same shape. section 5.10.2

The optimization depends on the shapes seen at one access site. One shape is monomorphic; two through four are polymorphic; more than four are megamorphic. Shape changes invalidate remembered locations, and repeated variation can force slower object handling. section 5.10.2

Use the state model to interpret both behavior and diagnostics:

  • Monomorphic access sites see one object shape and provide the strongest specialization. section 5.10.2
  • Polymorphic sites retain several shape-specific paths, with performance degrading as more shapes are added. section 5.10.2
  • Megamorphic sites have seen more than four shapes and can lose the relevant optimizations. section 5.10.2
  • Treat IC traces as runtime evidence rather than language semantics: Node.js exposes --log-ic and --trace-deopt, while tooling differs across runtimes. section 5.10.2 section 11.5

Watching one access site widen

For function readX(point) { return point.x; }, feed objects shaped with x plus a, b, c, d, e, and f. The access site moves from uninitialized to monomorphic, then polymorphic, and finally megamorphic. A Node.js 24 measurement reports 1.116 nanoseconds per load for one shape, 2.094 for four, and 5.414 for five. section 5.10.2

const shapes = [
  { x: 1, a: 0 }, { x: 2, b: 0 }, { x: 3, c: 0 },
  { x: 4, d: 0 }, { x: 5, e: 0 }, { x: 6, f: 0 },
];
 
let total = 0;
for (let limit = 1; limit <= shapes.length; limit++) {
  for (let i = 0; i < limit * 2; i++) total += readX(shapes[i % limit]);
}

Run the shape-variation loop to observe how repeated property loads expose IC state changes. section 5.10.2

How JavaScript in Depth covers it

How deep it goes
Explains cached locations, hidden classes, IC states, invalidation, deoptimization, logs, and benchmarked performance.
Examples
Uses a readX function, six differently shaped point objects, IC logs, and Node.js 24 property-load measurements.
What it assumes
Assumes familiarity with JavaScript objects and introduces hidden classes, object shapes, and monomorphic-to-megamorphic IC states.
What you can do after
Diagnose hot property-access slowdowns by relating object-shape diversity, IC states, profiles, and runtime-specific traces.
How it connects
Connects inline caches to hidden classes, object shapes, property access, deoptimization, and runtime diagnostics.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.