A JavaScript Engine is a program that:
- Reads JavaScript code (source code)
- Parses and compiles it
- Executes it on the machine
Different environments have different engines:
- Chrome / Node.js → V8
- Firefox → SpiderMonkey
- Safari → JavaScriptCore
💡 "JS Engine exposed" usually means:
- Understanding how the JS engine works internally (execution flow, memory handling, optimizations)
- Knowing which parts are exposed to developers (e.g., ECMAScript features)
- Recognizing what's not part of the JS engine (e.g., DOM APIs, setTimeout — these come from the environment, not the engine itself)
V8 is:
- Open-source JavaScript & WebAssembly engine
- Written in C++
- Used in:
- Google Chrome
- Node.js
- Deno
Known for:
- JIT compilation (Just-In-Time)
- Garbage Collection (automatic memory management)
- High performance
Here's the big picture:
┌─────────────────────────┐
│ JavaScript Source Code │
└───────────┬─────────────┘
│
1️⃣ Parser
│
┌───────────▼─────────────┐
│ Abstract Syntax Tree │
│ (AST) │
└───────────┬─────────────┘
│
2️⃣ Interpreter (Ignition)
│
┌───────────▼─────────────┐
│ Bytecode │
└───────────┬─────────────┘
│
Hot Code Detected
│
3️⃣ Optimizing Compiler (TurboFan)
│
┌───────────▼─────────────┐
│ Optimized Machine Code │
└─────────────────────────┘
- Takes JS source code and creates an AST (Abstract Syntax Tree).
- The AST is a structured representation of code, making it easier for the engine to analyze.
- Converts AST → Bytecode (lightweight instructions for the engine to execute).
- Runs the code immediately without waiting for full compilation.
- This gives fast startup.
- Watches code while it runs.
- If it sees a function running many times (a "hot" function), it optimizes it into highly efficient machine code.
- Makes repeated operations faster.
Heap → Stores objects, arrays, closures, etc.
Stack → Stores function calls and primitive variables
Garbage Collector (Orinoco / Oilpan):
- Automatically frees memory for objects that are no longer reachable
- Uses generational GC (young vs old objects)
- Uses incremental & concurrent GC to avoid blocking execution
Important: V8 only runs JavaScript.
It doesn't:
- Provide DOM APIs (document.querySelector, etc.)
- Provide setTimeout, fetch
Those come from the host environment:
- Browser → Web APIs
- Node.js → libuv & Node APIs
The runtime = V8 + host environment APIs + Event Loop.
"V8 is Google's high-performance open-source JavaScript and WebAssembly engine, written in C++. It parses JS into an AST, interprets it into bytecode with Ignition, and optimizes hot code into machine code with TurboFan. It also manages memory with an advanced garbage collector. V8 doesn't provide DOM or timers — those come from the host environment like browsers or Node.js, which, together with V8 and the Event Loop, form the full JavaScript runtime."
┌─────────────────────────┐
│ JavaScript Code │
└───────────┬─────────────┘
▼
┌───────────────────────┐
│ Parser │ → AST
└───────────┬───────────┘
▼
┌─────────────────────────┐
│ Ignition Interpreter │ → Bytecode
└───────────┬─────────────┘
▼
Hot Functions Detected
▼
┌─────────────────────────┐
│ TurboFan Compiler │ → Optimized Machine Code
└─────────────────────────┘