Compile Ready
Module 1 · JavaScript Fundamentals

Inside the V8 Engine

Intermediate8m read3m practice11m total
V8JITPerformanceInternals

Introduction

V8 is Google's open-source JavaScript engine, written in C++. It powers Chrome, Edge, and — crucially — Node.js and Deno. It is the engine most interview questions implicitly refer to.

V8 is famous for its two-tier compilation strategy that balances fast startup with peak performance.

Why This Matters

Senior interviews often drift into performance: "why is this loop slow?", "what is a hidden class?", "why keep object shapes consistent?". A working model of V8 lets you write JIT-friendly code and explain your reasoning.

Theory

The two-tier pipeline

  • Ignition — the interpreter. It compiles the AST to compact bytecode and executes it. This gives fast startup and low memory.
  • TurboFan — the optimising compiler. When Ignition's profiler flags a function as hot, TurboFan recompiles it to optimised machine code using assumptions gathered at runtime (e.g. "this argument is always a number").

Hidden classes (shapes)

JavaScript objects are dynamic, but V8 secretly assigns each object a hidden class describing its property layout. Objects created with the same properties in the same order share a hidden class, letting V8 use fast, array-like property access instead of a dictionary lookup.

Adding properties in different orders — or adding them after creation — creates new hidden classes and can force V8 to fall back to slow dictionary mode.

Inline caches

V8 remembers where a property was found last time (an inline cache). Stable object shapes keep these caches "monomorphic" (one shape) and fast; mixing many shapes makes them "megamorphic" and slow.

Practical rules for JIT-friendly code

  • Initialise all object properties in the constructor, in a consistent order.
  • Avoid deleting properties (delete obj.x) on hot objects.
  • Keep arrays packed and of a single element type where possible.
  • Don't change a variable's type across iterations of a hot loop.

Visual Diagrams

V8 two-tier compilation
               profiler marks 'hot'
AST --> [Ignition] --> bytecode --> execute
            ^                          |
            |    deoptimise            v
            +-------------------- [TurboFan] --> optimised machine code
                                   (uses runtime type assumptions)

Fast startup from Ignition, peak speed from TurboFan, safety via deopt.

Code Examples

Consistent object shapes help V8

Both objects below have the same properties in the same order, so V8 can give them the same hidden class — the fast path:

Loading…

Output Prediction

Predict the output #1

javascript
1const obj1 = {};
2obj1.a = 1;
3obj1.b = 2;
4
5const obj2 = {};
6obj2.b = 2;
7obj2.a = 1;
8
9console.log(obj1.a === obj2.a);

Interview Questions

1What is a hidden class in V8 and why should I care?

A hidden class (or "shape") is V8's internal description of an object's property layout. Objects with the same properties added in the same order share a hidden class, enabling fast, offset-based property access and effective inline caches. Creating objects with inconsistent shapes forces slower dictionary lookups. Practical takeaway: initialise all properties in the constructor, in a consistent order, and avoid delete on hot objects.

Asked at:GoogleMetaAmazon
2What are Ignition and TurboFan?

Ignition is V8's interpreter that compiles the AST to bytecode and runs it (fast startup, low memory). TurboFan is the optimising JIT compiler that recompiles hot functions to machine code using runtime type feedback, deoptimising if an assumption is later violated.

Quiz

1. Which practice is most JIT-friendly in V8?

Summary

  • V8 is Google's C++ engine powering Chrome, Edge, and Node.js.
  • Ignition interprets bytecode for fast startup; TurboFan JIT-compiles hot code.
  • Hidden classes let objects with identical shapes share fast property access.
  • Consistent object shapes and packed arrays keep code on V8's fast path.

Cheat Sheet

V8 tiers: Ignition (interpreter, bytecode) + TurboFan (optimising JIT).

Hidden class: internal object shape. Same props, same order → shared shape → fast.

Do: init props in constructor, consistent order; keep arrays packed/typed.

Avoid: delete on hot objects; changing a variable's type in hot loops.