Sitemap
Prince Kumar Chaurasia

Prince Kumar Chaurasia

Javascript Beyond Basics

--

Learning the depth of Javascript

JS Engine Internals (V8) + Optimization Magic

What happens when you write:

const x = 5 + 3;

It’s not just magically calculated. Here’s what happens:

Breakdown of JS Engine (like V8)

  1. Parsing
  • Converts your code → an Abstract Syntax Tree (AST)
  • Detects syntax errors
  • Uses a parser (like Acorn, Esprima)

2. Compilation (not interpretation!)

    Get Prince Kumar Chaurasia’s stories in your inbox

    Join Medium for free to get updates from this writer.

  • Modern JS engines compile JS to machine code
  • Uses Just-In-Time (JIT) compilation
  • First: Ignition → bytecode
  • Then: TurboFan → optimized native code

3. Execution

  • Your code runs in bytecode/natively optimized machine instructions

4. Optimization

  • V8 watches how your code behaves
  • If a function runs often with the same type of data → it optimizes
  • If behavior changes (e.g., types flip) → it de-optimizes!

V8 Pipeline

Source Code → Parser → AST
↓
Interpreter (Ignition) → Bytecode
↓
Profiler watches for "hot code"
↓
Optimizing Compiler (TurboFan) → Native Machine Code

Inline Caching

JS is dynamic, but V8 assumes things stay the same for speed.

function greet(user) {
return "Hello " + user.name;
}

V8 caches the user.name lookup. If later you change the object’s shape — it has to throw away that optimization.

--

--