Inside the V8 Engine: A Deep Dive into JavaScript Execution
A walkthrough of how the V8 engine runs JavaScript in Chrome and Node.js. You follow a script from the parser to Ignition bytecode, through the Sparkplug, Maglev and TurboFan compilers, then see how hidden classes, inline caches and deoptimization work, with a demo you can run in Node.js 22 to watch a function get optimized and then thrown back to the interpreter.

Most of the time you can treat the JavaScript engine as a black box: code goes in, things happen. That stops being enough when a hot loop in a Node.js service or a busy page in Chrome runs slower than it should. This guide is for JavaScript developers who want to know what V8 does with their code, from parsing to machine code, and which coding habits help it.
V8 is Google's open source JavaScript and WebAssembly engine, written in C++, and it runs JavaScript in Chrome and Node.js. It doesn't just interpret your code. It uses just-in-time (JIT) compilation: it turns frequently run code into machine code while the program runs.
This is part 13 of the Frontend & JavaScript Fundamentals series. The previous part covered async/await and generators, which run on top of everything described here.
Key takeaways
- V8 parses your code, runs it as Ignition bytecode, and compiles hot functions with faster tiers: Sparkplug, then Maglev and TurboFan.
- The optimizing compilers guess types from feedback. When a guess turns out wrong, V8 deoptimizes and goes back to the interpreter.
- Create objects with the same properties in the same order, and pass the same kinds of values to a function. That keeps V8's shortcuts working.
The V8 pipeline at a glance
When V8 gets a script, it doesn't compile everything to machine code up front, because that would slow startup. It doesn't stay in a slow interpreter forever, either. The pipeline has four stages:
- Parsing: the source becomes an Abstract Syntax Tree (AST).
- Interpretation (Ignition): the AST becomes bytecode, and Ignition runs it.
- Baseline compilation (Sparkplug): bytecode is turned into machine code quickly, without optimizing it.
- Optimizing compilation (Maglev and TurboFan): hot functions get machine code built on what V8 has seen at runtime.
The earlier version of this post described only Ignition and TurboFan. Sparkplug was added in 2021 and Maglev shipped in Chrome 117, so the summary above follows the V8 blog today.
| Tier | What it does | Trade-off |
|---|---|---|
| Ignition | Interprets bytecode and collects type feedback | Starts at once, runs slowest |
| Sparkplug | Turns bytecode into machine code in one linear pass | Very fast to compile, no optimizations |
| Maglev | Optimizing compiler that uses the feedback | "Good enough code, fast enough"; about 10 times slower to compile than Sparkplug |
| TurboFan | Optimizing compiler for peak speed | Best code, about 10 times slower to compile than Maglev |
The timing figures in the table come from the V8 blog post Maglev, V8's fastest optimizing JIT.
Node.js 22 turns Maglev off
Node.js 22.0.0 enabled Maglev, but the Node.js 22.9.0 release notes disabled it by default on v22. In Node.js 22.23, node --v8-options lists --maglev as off, so hot functions skip that tier. In Node.js 23.11, the same command lists it as on. Check your own runtime with node --v8-options.
1. Parsing and the AST
The first step is lexical analysis. The scanner breaks your source text into tokens. For example, const a = 10; becomes a keyword (const), an identifier (a), an operator (=), a number literal (10) and a semicolon.
The parser turns those tokens into an Abstract Syntax Tree (AST), a tree that shows how the code is structured.
V8 doesn't fully parse every function at once. The V8 blog on lazy parsing explains two modes:
- Lazy parsing: for a function that isn't called yet, a lighter preparser only checks the syntax and notes which variables it uses. When the function is first called, V8 parses and compiles it fully.
- Eager parsing: functions that are likely to run straight away, such as a function wrapped in parentheses and called at once, are parsed and compiled up front.
Lazy parsing saves startup time and memory. The same post puts it plainly: "V8 without lazy compilation significantly regresses load time".
2. Ignition: the interpreter
Next, Ignition comes in. Ignition is V8's interpreter. The AST is turned into bytecode, a compact set of instructions that Ignition runs one by one.
Bytecode is much smaller than machine code. When Ignition launched, the V8 team said its bytecode was 25% to 50% of the size of the baseline machine code it replaced. That matters on phones with little memory.
At this stage your JavaScript is already running. It isn't fast yet, but it started quickly. Code that runs only once may never leave Ignition.
3. The feedback vector and Sparkplug
While Ignition runs your code, it also takes notes. For each function, it fills a feedback vector: a record of what each operation has seen, such as the shapes of objects and the types of values.
V8 is looking for hot code: functions that run often or contain busy loops. When a function becomes hot, V8 hands it to a faster tier.
Sparkplug is the first step up. The V8 blog describes it as compiling bytecode to machine code almost instantly, in a single pass. It does no clever optimizing. It just removes the cost of decoding each bytecode instruction.
4. Maglev and TurboFan: the optimizing compilers
The optimizing compilers take the bytecode plus the feedback vector and make speculative optimizations.
Why "speculative"? JavaScript is dynamically typed. V8 can never be sure that add(a, b) will always get small integers. But if the feedback says it always has so far, the compiler builds machine code for integers only. That code skips the type checks and lookups that the interpreter has to do.
Maglev compiles fast and produces good code. TurboFan takes longer and produces the best code, so V8 saves it for the hottest functions. TurboFan was built around a graph called the "Sea of Nodes". The V8 blog post "Land ahoy: leaving the Sea of Nodes" (March 2025) explains that its JavaScript backend now uses Turboshaft, a more traditional control-flow graph. In Node.js 22.23, --trace-opt shows TurboFan compiling in the background ("ConcurrencyMode::kConcurrent"), so the main thread keeps running.
Hidden classes (shapes) and inline caching
To write code that optimizes well, you need to know how V8 handles objects. In a static language such as C++, an object's memory layout is fixed. In JavaScript you can add properties at any time. A naive engine would look up every property in a dictionary, which is slow.
V8 avoids this with hidden classes, which V8 calls Maps. Other engines and articles call them shapes.
The shape transition chain
Consider this code:
function Point(x, y) {
this.x = x;
this.y = y;
}
const p1 = new Point(1, 2);
Here is what happens inside V8:
- Map0: an empty object is created.
- Map1: when
this.x = xruns, the object moves to a new Map that addsxin the object's first slot. - Map2: when
this.y = yruns, it moves to Map2, which addsyin the second slot.
V8's documentation calls the step from one Map to the next a transition. If you then create new Point(3, 4), it follows the same transitions and ends up with the same Map2. V8 can now read x from a fixed slot, without a dictionary lookup.
Inline caching (IC)
The shape is what makes inline caching possible. When V8 runs p1.x, it remembers the object's Map and where x was found. The next time the same line runs, the check becomes: "if the object has Map2, x is in the first slot." Ignition stores this cache state in the feedback vector, and the optimizing compilers build on it.
Deoptimization: the bailout
Speculative code is built on assumptions. Here is what happens when one breaks:
function add(a, b) {
return a + b;
}
// Hot code: V8 optimizes 'add' for integers
for (let i = 0; i < 100000; i++) {
add(i, i);
}
// Curveball: Passing a string
add("Hello", "World");
In Node.js 22, by the end of the loop, TurboFan has compiled add into machine code that expects small integers. When add("Hello", "World") runs, a type check in that code fails.
That triggers deoptimization:
- V8 drops the optimized code for
add. - Execution goes back (bails out) to Ignition, which runs the bytecode.
- The bytecode handles the strings correctly, just more slowly.
- The feedback now records that
addhas seen strings as well as numbers. Ifaddstays hot, V8 can optimize it again with wider assumptions.
You can watch this happen. Save the code as deopt.js and run node --trace-opt --trace-deopt deopt.js. In Node.js 22.23 the log shows add being compiled with TurboFan (Maglev is off there), then a bailout with the reason not a Smi. With a much shorter loop, add may not finish optimizing before the string call, and you won't see the bailout.
A single deoptimization is cheap. As a rule of thumb, the cost that hurts is a function that keeps getting optimized and deoptimized because its inputs keep changing.
Writing V8-friendly code
These internals lead to a few habits that help V8 keep your code fast.
1. Initialize properties in the same order
Give objects the same properties in the same order, ideally in the constructor. Then they share one hidden class. If you add properties in a different order, you get different shapes, and the inline caches have more cases to handle.
// BAD: same properties, different order, two shapes
const a = { x: 1 };
a.y = 2;
const b = { y: 2 };
b.x = 1;
// GOOD: every Point gets x, then y
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
You can check this in Node.js with one of V8's internal test functions. Add these lines below the code above and run it with node --allow-natives-syntax shapes.js. The % functions are for experiments only and are not part of JavaScript.
console.log(%HaveSameMap(a, b)); // false
console.log(%HaveSameMap(new Point(1, 2), new Point(3, 4))); // true
2. Keep functions monomorphic
An operation is monomorphic when it always sees objects of one shape. It's polymorphic when it sees a few shapes, and megamorphic when it sees too many to track. In Node.js 22.23, node --v8-options sets the limit at four shapes (--max-valid-polymorphic-map-count=4).
Monomorphic code is fastest, because the cache only checks one shape. Megamorphic code falls back to a slower, general lookup. So pass objects of the same shape to the same function where you can.
3. Know about small integers (Smi vs HeapNumber)
V8 stores small integers, called Smis, directly in the value slot with a tag bit, with no separate object. Larger numbers and decimals usually go into a separate box called a HeapNumber.
How small is small? It depends on the build. Pointer compression cuts Chrome's renderer memory by up to 20%, the V8 post reports. With it, Smis are limited to 31 bits. Without it, 64-bit builds can use 32 bits. In Node.js 22.23, %IsSmi(2 ** 31 - 1) returns true, so that build uses the larger size. As a rule of thumb, don't contort your code for this. Just avoid mixing integers and decimals in the same hot field or array when speed really matters.
Summary
V8 starts your code quickly with the parser and Ignition. It then speeds up hot code with Sparkplug, Maglev and TurboFan, based on what it has seen. Hidden classes and inline caches make property access fast. Deoptimization is the safety net when a guess is wrong.
You don't need to write code for the engine most of the time. When a hot path is slow, though, consistent object shapes and consistent argument types are the first things to check.
Next in the series, CORS, HTTP and web security moves from the engine to the network: what happens when your code calls another server.
Frequently asked questions
Is JavaScript interpreted or compiled?
Both. In V8, your code is first compiled to bytecode and interpreted by Ignition. Functions that run often are then compiled to machine code by Sparkplug, Maglev or TurboFan while the program runs. That mix is what "just-in-time compilation" means.
What is the difference between Ignition and TurboFan?
Ignition is the interpreter. It starts quickly, uses little memory and collects type feedback. TurboFan is the optimizing compiler for the hottest code. It uses that feedback to build fast machine code, and gives the work back to Ignition when its assumptions break.
What causes deoptimization in V8?
Optimized code is built on guesses from earlier runs, such as "this value is always a small integer" or "this object always has this shape". When a value breaks a guess, V8 drops the optimized code and returns to the interpreter. Changing argument types and changing object shapes are the usual causes.
Do hidden classes matter in everyday code?
Usually not much. They matter in hot paths: loops over many objects, or functions called thousands of times. There, objects built with the same properties in the same order let V8's inline caches stay monomorphic.
Does Node.js use the same V8 tiers as Chrome?
Node.js uses V8 too, but each Node.js release pins a V8 version and its own flags. For example, Node.js 22.9.0 and later 22.x releases turn Maglev off by default. Run node --v8-options to see what your version uses.
Sources
Verified against the sources below on October 30, 2026. Products and docs change often: check the linked sources if something looks different.
- V8 JavaScript engine (v8.dev)
- V8 blog: Blazingly fast parsing, part 2: lazy parsing
- V8 blog: Firing up the Ignition interpreter
- V8 blog: Sparkplug, a non-optimizing JavaScript compiler
- V8 blog: Maglev, V8's fastest optimizing JIT
- V8 docs: TurboFan
- V8 blog: Land ahoy, leaving the Sea of Nodes
- Node.js 22.0.0 release notes
- Node.js 22.9.0 release notes (Disable V8 Maglev)
- V8 docs: Maps (hidden classes) in V8
- V8 blog: Pointer compression in V8
- Mathias Bynens and Benedikt Meurer: JavaScript engine fundamentals, Shapes and Inline Caches
All 30 parts in this series
- Part 01Overcoming the Daunting Start: A Strategic Roadmap for New Frontend Developers
- Part 02Beyond the Tags: Mastering HTML Architecture for Product-Based Roles
- Part 03From Selectors to BEM: CSS Concepts You Must Know for Professional Projects
- Part 04Mastering the Primitives: A Builder's Guide to JavaScript Data Types and Functions
- Part 05Demystifying Scope and Closure: Key Concepts for Writing Robust JavaScript
- Part 06Beyond Frameworks: Practical DOM Manipulation Techniques for High-Performance UIs
- Part 07From Prototypes to ES6 Classes: Understanding JavaScript's Object-Oriented Nature
- Part 08Architecting Scalable JavaScript: OOP Principles & Design Patterns
- Part 09Mastering the Metal: Essential Web APIs & Browser Internals for Frontend Engineers
- Part 10Understanding the JavaScript Runtime: Event Loop, Call Stack, and Task Queue Explained
- Part 11Mastering Asynchronous Operations with JavaScript Promises: A Builder's Guide
- Part 12Modern Async Patterns: Making Asynchronous Code Readable with Async/Await and Generators
- Part 13Inside the V8 Engine: A Deep Dive into JavaScript Execution
- Part 14Demystifying CORS, HTTP, and Security: A Builder’s Guide
- Part 15REST vs. GraphQL: A Frontend Engineer’s Reality Check
- Part 16Secure Frontend Auth: The Definitive Guide to JWTs, Cookies, and Storage Strategies
- Part 17Why TypeScript Matters: Adding Type Safety to Your JavaScript Projects
- Part 18Getting Started with React: Deconstructing JSX and Component Architecture
- Part 19React Architecture: Mastering State and Props in Class vs. Functional Components
- Part 20Mastering React Hooks: From useState to Building a Production-Ready Custom Hook
- Part 21React State Management: Context API vs. Redux & Building a Theme Switcher
- Part 22React Architecture: Advanced Navigation and Data Flow Patterns
- Part 23Automated Testing in React: Unit Testing with Jest and React Testing Library
- Part 24Mastering Chrome DevTools: Debugging Techniques That Save Hours of Development Time
- Part 25Automating Code Quality: The Modern Stack with ESLint, Prettier, and Husky
- Part 26Demystifying the Build Pipeline: A Deep Dive into Webpack and Babel Configuration
- Part 27Improving Performance with Server-Side Rendering: Concepts and Implementation
- Part 28From Development to Production: A Builder's Guide to Frontend Deployment Strategies
- Part 29Introduction to Micro Frontends: Building Scalable Architectures
- Part 30The Frontend Architecture: From Junior Developer to Tech Lead
AI Automation Engineer & Salesforce Developer
Salesforce developer since June 2018. I build AI agents, n8n workflows and Salesforce integrations, and write up what holds up in real use.



Comments
Loading comments...