The JavaScript engine

The V8 JavaScript Engine

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 we browse with Chrome.

V8 provides the runtime environment in which JavaScript executes. The DOM and the other Web Platform APIs are not part of it. The browser provides those.

The cool thing is that the JavaScript engine is independent of the browser that hosts it. This is what made Node.js possible. V8 was chosen as the engine for Node.js back in 2009, and as Node.js exploded in popularity, V8 became the engine behind an incredible amount of server-side JavaScript.

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

Other JS engines

Other browsers have their own JavaScript engine:

  • Firefox has SpiderMonkey
  • Safari has JavaScriptCore (also called Nitro)
  • Edge had Chakra, until it switched to Chromium and V8 in 2020

and many others exist as well.

All those engines implement the ECMA-262 standard, also called ECMAScript, the standard behind JavaScript.

The quest for performance

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

In this introduction I’ll skip the implementation details. You can find them on the V8 official site, and they change over time, often radically.

V8 keeps evolving, like every other JavaScript engine, to speed up the Web and the Node.js ecosystem. There’s been a race for performance going on for years, and we benefit from it as users and as developers. Our code gets faster year after year without us changing a line.

Compilation

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

This started in 2009, when the SpiderMonkey JavaScript compiler was added to Firefox 3.5, and everyone followed.

V8 compiles JavaScript with just-in-time (JIT) compilation: it compiles code while the program runs, using what it observes about the code to make it faster.

This might seem counter-intuitive. But since Google Maps arrived in 2004, JavaScript has grown from a language running a few dozen lines to complete applications with hundreds of thousands of lines running in the browser. Our apps now run for hours in a tab, rather than validating a form.

In this new world, compiling makes perfect sense. It takes a little longer to get the JavaScript ready, but once it’s done, it runs much faster than purely interpreted code.

Lesson completed