V8 Engine Internals
How V8 runs JavaScript fast — parsing, the Ignition interpreter, Sparkplug, Maglev and TurboFan JITs, hidden classes (shapes), inline caches, deoptimisation, element kinds, and Orinoco garbage collection — plus how to write engine-friendly code.
V8 is Google's JavaScript and WebAssembly engine, used by Chrome, Node.js, Deno and Edge. Other engines (SpiderMonkey in Firefox, JavaScriptCore in Safari, used by Bun) use very similar ideas. Knowing them explains why some code is 10× faster than equivalent-looking code.
The pipeline
| Tier | What it does | Trade-off |
|---|---|---|
| Parser | Builds an AST. Lazy parsing: inner functions are only pre-parsed (syntax-checked) until first called | Faster start-up |
| Ignition | Compiles the AST to compact bytecode and interprets it (a register machine), collecting type feedback | Starts instantly, slowest execution |
| Sparkplug | Translates bytecode straight to machine code with no optimisation | Very fast compile, removes interpreter overhead |
| Maglev | Fast optimising compiler using feedback | Good code quickly |
| TurboFan | Aggressive optimising compiler (inlining, escape analysis, type specialisation) | Slow compile, fastest code |
Like the JVM (JIT compilation), V8 speculates based on observed types and deoptimises back to Ignition when a speculation fails.
Hidden classes (shapes / maps)
JavaScript objects are dynamic dictionaries in principle, but dictionaries are slow. V8 instead gives every object a hidden class ("map" in V8; "shape" elsewhere) describing its layout: which properties exist and at which offset they're stored. Objects created the same way share a hidden class, so property access becomes "load at offset N" — like a C struct.
Adding properties creates transitions between hidden classes:
function Point(x, y) {
this.x = x; // shape: {} → {x}
this.y = y; // {x} → {x, y}
}
const a = new Point(1, 2);
const b = new Point(3, 4); // same shape as a: fast, sharedOrder matters: {x, y} and {y, x} are different shapes.
const p1 = { x: 1, y: 2 };
const p2 = { y: 2, x: 1 }; // different shape from p1
const p3 = { x: 1 };
p3.y = 2; // same final shape as p1 (same transitions)
delete p1.x; // may drop p1 into slow "dictionary mode"Inline caches
Each property access site in your code (obj.x) has an inline cache (IC) that remembers the shapes it has seen and the offset for each:
| IC state | Shapes seen | Speed |
|---|---|---|
| Monomorphic | 1 | Fastest: check shape, load offset |
| Polymorphic | 2–4 | Small linear check |
| Megamorphic | More than 4 | Global hash-table lookup — much slower, and TurboFan can't specialise |
function getX(p) { return p.x; }
getX({ x: 1 }); // monomorphic
getX({ x: 1, y: 2 }); // polymorphic (2 shapes)
getX({ y: 2, x: 1 }); getX({ x: 1, z: 3 }); getX({ a: 0, x: 1 }); // drifting toward megamorphicElement kinds (arrays)
V8 tracks what an array contains and picks the most efficient representation:
| Kind | Example | Storage |
|---|---|---|
PACKED_SMI_ELEMENTS | [1, 2, 3] | Small integers, unboxed |
PACKED_DOUBLE_ELEMENTS | [1.5, 2, 3] | Raw doubles |
PACKED_ELEMENTS | [1, "a", {}] | Tagged pointers |
HOLEY_* variants | [1, , 3] or new Array(10) | Extra hole checks on access |
| Dictionary elements | a[1_000_000] = 1 on a small array | Hash table — slow |
Transitions only go towards the more general kinds: once an array holds a double or becomes holey, it never goes back.
Garbage collection: Orinoco
V8's heap is generational, like the JVM's (JVM GC):
- Young generation (small, ~16 MB): collected by the Scavenger, a parallel copying collector. Most objects die here.
- Old generation: Mark-Compact with concurrent marking (on helper threads while JS runs), incremental steps, parallel compaction, and lazy sweeping, keeping main-thread pauses short.
- Idle-time GC: Chrome schedules GC work in idle periods between frames.
Node's default old-space limit depends on system memory; raise it with --max-old-space-size=4096 (MB) for memory-hungry processes.
Writing engine-friendly code
node --trace-opt --trace-deopt app.js # see what gets optimised and why it deoptimises
node --allow-natives-syntax # %HasFastProperties(obj), %GetOptimizationStatus(fn) for experimentsOther engine features
- Code caching: compiled bytecode is cached across page loads (and in Node via
v8.compileCache/ snapshots) to speed start-up. - Snapshots: Node and Deno boot from a pre-initialised heap snapshot.
- WebAssembly: Liftoff (baseline) then TurboFan compile Wasm modules.
- Pointer compression: 32-bit compressed pointers in a 4 GB "cage" save ~40% heap memory on 64-bit systems.
- V8 parses lazily, interprets bytecode in Ignition, and tiers up through Sparkplug, Maglev and TurboFan using type feedback.
- Hidden classes give objects fixed layouts; same construction order = same shape.
- Inline caches make monomorphic property access fast; megamorphic sites are slow.
- Arrays have element kinds; keep them packed and homogeneous.
- Orinoco GC: parallel scavenger for young objects, concurrent mark-compact for old.
- Engine-friendly code is consistent code: same shapes, no deletes, no holes — and profile before optimising.
QWhat are hidden classes and why do they matter?hard
V8 assigns each object a hidden class describing its property layout. Objects created with the same properties in the same order share a hidden class, so property accesses compile to fixed-offset loads and inline caches can be monomorphic. Adding properties in different orders, adding them after construction, or deleting them creates divergent or dictionary-mode shapes, making accesses polymorphic or megamorphic and preventing optimisation.