The V8 JavaScript Engine

By

Published · Updated

Tagged: JavaScript

Understand V8, the JavaScript engine that powers Google Chrome and Node.js, how it provides the runtime, and how it uses just-in-time compilation for speed.

V8 is the name of the JavaScript engine that powers Google Chrome. It’s the thing that takes our JavaScript and executes it while browsing with Chrome.

V8 provides the runtime environment in which JavaScript executes. The DOM, and the other Web Platform APIs are provided by the browser.

The cool thing is that the JavaScript engine is independent of the browser in which it’s hosted. This key feature enabled the rise of Node.js. V8 was chosen to be the engine that powered Node.js back in 2009, and as the popularity of Node.js exploded, V8 became the engine that now powers an incredible amount of server-side code written in JavaScript.

The Node.js ecosystem is huge and thanks to it V8 also powers desktop apps, with projects like Electron.

Node.js 26, the current LTS line, ships V8 14, and Node.js 24, the previous LTS line now in maintenance, ships V8 13. The exact build string changes with every Node release. You can print it at runtime with process.versions.v8:

node -p process.versions.v8

In Chrome, open chrome://version to see the V8 version it ships.

Other JS engines

Other browsers have their own JavaScript engine:

and many others exist as well. Engine choice follows the browser: Chrome alternatives compared groups desktop browsers by engine and features.

All those engines implement the ECMA ES-262 standard, also called ECMAScript, the standard used by JavaScript.

The quest for performance

V8 is written in C++, and it’s continuously improved. It is portable and runs on Mac, Windows, Linux and several other systems.

In this V8 introduction, I will ignore the implementation details of V8: they can be found on more authoritative sites (e.g. the V8 official site), and they change over time, often radically.

V8 is always evolving, just like the other JavaScript engines around, to speed up the Web and the Node.js ecosystem.

On the web, there is a race for performance that’s been going on for years, and we (as users and developers) benefit a lot from this competition because we get faster and more optimized machines year after year.

Compilation

JavaScript is generally considered an interpreted language, but modern JavaScript engines no longer just interpret JavaScript, they compile it.

This happens since 2009 when the SpiderMonkey JavaScript compiler was added to Firefox 3.5, and everyone followed this idea.

JavaScript is internally compiled by V8 with just-in-time (JIT) compilation to speed up the execution.

This might seem counter-intuitive, but since the introduction of Google Maps in 2004, JavaScript has evolved from a language that was generally executing a few dozens of lines of code to complete applications with thousands to hundreds of thousands of lines running in the browser.

Our applications now can run for hours inside a browser, rather than being just a few form validation rules or simple scripts.

In this new world, compiling JavaScript makes perfect sense because while it might take a little bit more to have the JavaScript ready, once done it’s going to be much more performant than purely interpreted code.

How V8 runs your code

V8 doesn’t use a single compiler. It has several tiers, and a function moves up as it gets used more:

  1. Ignition is the interpreter. V8 parses your code, turns it into bytecode, and Ignition runs it. It starts fast and uses little memory.
  2. Sparkplug is a quick, non-optimizing compiler. It turns bytecode into machine code without trying to be clever. It shipped in Chrome 91, in 2021.
  3. Maglev is a fast optimizing compiler. It produces good code quickly, and it arrived in Chrome 117, in 2023.
  4. TurboFan is the top tier. It takes longer to compile, and produces the fastest code, for functions that run a lot.

Ignition and TurboFan became the main pipeline in V8 5.9 (Chrome 59, 2017), replacing the older Full-codegen and Crankshaft compilers. Sparkplug and Maglev were added later in between, so a function that runs a few times doesn’t have to wait for TurboFan.

The idea is simple: most code runs only a few times, so it’s not worth spending time optimizing it. The hot functions get compiled again, better.

Deoptimization

The optimizing compilers make guesses based on what they’ve seen so far. If a function has only ever received numbers, V8 compiles a version that assumes numbers.

When a guess turns out wrong, V8 throws away the optimized code and goes back to running that function in the interpreter. That’s called deoptimization.

You can watch it happen in Node.js:

// deopt.js
function add(a, b) {
  return a + b
}

for (let i = 0; i < 1e6; i++) add(i, 1)

add('a', 'b')
node --trace-opt --trace-deopt deopt.js

Among the output you’ll see V8 mark add for optimization because it’s “hot and stable”, and then a bailout line with reason: not a Smi when it gets two strings. A Smi is V8’s name for a small integer. The exact output changes between versions.

One deoptimization is no big deal. A function that keeps getting optimized and deoptimized in a loop is slow, and that’s usually a sign it receives very different kinds of values.

Hidden classes

JavaScript objects can get new properties at any time, which makes it hard to know where a property lives in memory. V8 solves this by giving every object a hidden class (internally called a “map”). It describes the object’s shape: which properties it has, in which order.

Objects with the same properties added in the same order share the same hidden class, and the optimized code for them stays fast. So create your objects the same way every time:

// same shape
const a = { x: 1, y: 2 }
const b = { x: 3, y: 4 }

// different shape: same properties, different order
const c = { y: 4, x: 3 }

A constructor or a class helps here, because it assigns the same properties in the same order for every object it creates.

Don’t go rewriting working code over this. It matters in hot paths that run millions of times, and a profiler tells you when you’re there.

Garbage collection

V8 frees the memory of objects you can’t reach anymore. Its collector is generational, built on the observation that most objects die young:

The Orinoco project turned the collector from one that stopped your program for the whole collection into a mostly parallel and concurrent one, so the pauses are much shorter.

In Node.js, a common error with big data sets is JavaScript heap out of memory. You can give the old generation more room, in megabytes:

node --max-old-space-size=4096 app.js

That helps when you really need the memory. If memory grows forever, you have a leak, and more space only delays the crash.

V8 flags in Node.js

Node.js passes V8 flags straight through, like the ones above. To see all of them:

node --v8-options

The list is long, and many flags are meant for V8’s own developers. They can change or disappear between versions, so don’t rely on the obscure ones in production.

Want me to talk about your product? You can sponsor this site.

Related posts about js: