How it works
The explanation, with examples you can run.
The tiers
| Tier | What it is | Trade-off |
|---|---|---|
| Ignition | Interpreter for V8's bytecode | Starts instantly, runs slowest |
| Sparkplug | Baseline compiler: bytecode straight to machine code | Fast to compile, no clever optimisation |
| Maglev | Mid-tier optimising compiler | Good code, compiled quickly |
| TurboFan | Top-tier optimising compiler | Best code, most expensive to produce |
While code runs, V8 records feedback: which shapes of objects reached each property access, which types each operation saw. The optimising tiers bet on that feedback staying true.
Shapes and inline caches
V8 gives objects with the same properties, added in the same order, the same hidden class (V8 calls it a map). A property access that only ever sees one shape can be compiled to a single memory load. Seeing many shapes at the same spot makes it slower.
// Same shape every time: fields set in the constructor, in the same order.class Point { constructor(x, y) { this.x = x this.y = y }} // Shapes that differ by creation order, or by fields added later, make the same code see many shapes.const a = { x: 1, y: 2 }const b = { y: 2, x: 1 } // a different shape from aWhat actually helps
- Initialise all fields in the constructor; avoid adding or
delete-ing properties on hot objects later. - Keep a variable or array to one kind of value (don't mix numbers and strings in a hot array).
- Measure before tuning: the Performance panel in DevTools, or
node --cpu-prof, shows where time really goes.
Check yourself
2 questions. Get them all right to finish this topic.
0 of 2 right so far. Get all two to finish this topic.