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.

Advanced 4 min readLesson 8 of 12 Interview Q&A

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

warmhotvery hotdeoptSourceParserAST (lazy)Ignitionbytecode interpreterSparkplugbaseline compilerMaglevmid-tier optimisingTurboFantop-tier optimisingFeedback vectorstypes seen
TierWhat it doesTrade-off
ParserBuilds an AST. Lazy parsing: inner functions are only pre-parsed (syntax-checked) until first calledFaster start-up
IgnitionCompiles the AST to compact bytecode and interprets it (a register machine), collecting type feedbackStarts instantly, slowest execution
SparkplugTranslates bytecode straight to machine code with no optimisationVery fast compile, removes interpreter overhead
MaglevFast optimising compiler using feedbackGood code quickly
TurboFanAggressive 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:

JavaScript
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, shared
add xadd yShape 0{}Shape 1{x @0}Shape 2{x @0, y @1}

Order matters: {x, y} and {y, x} are different shapes.

JavaScript
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 stateShapes seenSpeed
Monomorphic1Fastest: check shape, load offset
Polymorphic2–4Small linear check
MegamorphicMore than 4Global hash-table lookup — much slower, and TurboFan can't specialise
JavaScript
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 megamorphic

Element kinds (arrays)

V8 tracks what an array contains and picks the most efficient representation:

KindExampleStorage
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 elementsa[1_000_000] = 1 on a small arrayHash 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

Shell
node --trace-opt --trace-deopt app.js   # see what gets optimised and why it deoptimises
node --allow-natives-syntax             # %HasFastProperties(obj), %GetOptimizationStatus(fn) for experiments

Other 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.
Key takeaways
  • 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.
Interview questions
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.