How Google V8 Engine Turbocharged JavaScript Nodejs Performance?

JavaScript's V8 engine powers Chrome and Node.js. Learn its key features like just-in-time compilation, hidden classes, and inline caching that make JavaScript blazing fast.

Believe it or not, JavaScript was once painfully slow. We're talking dial-up modem slow. Executing at a snail's pace. So what changed to make it insanely fast today? A little engine called V8.

V8 is the supercharged engine underneath Google Chrome and Node.js that takes JavaScript and juices up its performance beyond belief. Intrigued about what makes V8 so special? Settle in and I'll explain how Google cracked the code on speeding up JavaScript.

From Interpreted Script to Optimizing Dynamo

In JavaScript's early days, it was slow for one key reason - it was interpreted. Every time a JavaScript program ran, an interpreter would parse and analyze each line one by one before executing it.

This line-by-line interpretation made execution really inefficient. It was like reading a recipe from start to finish every time you wanted to bake a cake rather than remembering the steps!

The clever folks at Google realized this needed to change if JavaScript was ever going to compete for speed. So in 2008, they set out to create a new JavaScript engine, one designed for performance from the ground up. That engine was V8.

When V8 landed in Chrome, it blew developers' socks off. JavaScript was suddenly lighting fast, running circles around traditional interpreted languages. How did V8 work its magic? Some very savvy optimizations.

Just-in-Time Compilation

The core breakthrough that supercharged JavaScript performance in V8 was just-in-time (JIT) compilation.

Rather than interpreting JavaScript code each time, V8 would instead profile scripts as they ran to detect hot code paths - parts of the code executed frequently. V8 could then JIT compile those specific hot paths to efficient machine code optimized for your CPU.

This meant only the most important code paths ran at full native speed, while less critical code stayed interpreted. It combined the best of interpretation and compilation!

JIT compilation turbocharged JavaScript's performance, leaving traditionally interpreted languages like Python and Ruby far behind. V8 absolutely deserved its "dynamo" nickname after this innovation!

Hidden Classes: V8's Secret Weapon for Speed

But the V8 team didn't stop with JIT compilation. Next, they looked at optimizing JavaScript's dynamic object model to allow faster property access.

Their solution was hidden classes. V8 maintains a hidden class for each object shape and allows objects with the same "shape" - property names and types - to share a class.

This means similar objects can share efficient internal property storage. And property lookups are lightning quick because V8 knows right where to look based on an object's hidden class. Pretty clever!

So V8 leveraged hidden classes to optimize JavaScript's dynamic prototypes behind the scenes without impacting developers. This let JavaScript get speedy object access like statically typed languages enjoy.

Inline Caching: V8's Lightning Property Access

The V8 team had one more ace up their sleeve when it came to property access - inline caching. V8 optimizes common object property lookups using inline caches.

The first time V8 encounters a property access, like, it remembers the lookup details. The next time around, V8 skips the prototype chain traversal and grabs the value straight from the inline cache!

This makes repeated property accesses blindingly fast. V8 just needed that first lookup to "prime" the cache. Inline caching is why you can loop through objects millions of times in JavaScript today without a hiccup.

Multi-Tier Pipeline: Optimizing Across Code Spectrum

With JIT, hidden classes, and inline caching, V8 had solutions tailored for different types of JavaScript code. But it needed a way to tie these together.

The answer was a multi-tier execution pipeline. V8 uses multiple tiers - an interpreter, baseline compiler, and optimizing compiler - to balance startup time, memory use, and peak performance.

Less critical code stays interpreted for quick startup and low memory use. Hot code paths get baseline compiled for moderate speedup. And the most critical code goes through V8's full optimization pipeline to get max performance via JIT compilation.

This spectrum of execution tiers allowed V8 to unlock great JavaScript performance across a huge variety of apps and use cases.

Crankshaft: V8’s First Optimizing Compiler

When V8 was first built, it only had an interpreter. But the team knew an optimizing compiler was needed to maximize performance. In 2010, they introduced Crankshaft, V8’s first optimizing JIT compiler.

Crankshaft performed sophisticated optimizations like inlining functions, dead code elimination, and loop-invariant code motion to generate highly optimized machine code from the interpreter's output. It included both a "full" compiler for maximum optimization as well as a "light" compiler for faster compilation.

Crankshaft was a huge step forward, boosting performance of compute-heavy JavaScript applications by up to 5X compared to the interpreter alone.

TurboFan: Next-Gen Optimization in V8

By 2013, V8 was ready for even better optimization. The team introduced TurboFan to replace Crankshaft as V8's optimizing compiler.

TurboFan improved on Crankshaft's design and brought new state-of-the-art optimizations to V8 including:

  • Hydrogen IR: A high-level intermediate representation that simplified analysis and optimization.
  • Escape analysis: Tracks object scope to reduce memory allocation and enable scalar replacement.
  • Machine code optimization: Intelligently re-optimizes generated machine code on the fly.
  • Wasm compiler: Added the ability to compile WebAssembly to further boost performance.

Together, these innovations in TurboFan pushed JavaScript performance even closer to native speeds. Anything from large web apps to 3D games could now run at blazing speeds using TurboFan's advanced optimizations.

Liftoff: Faster Startup With Baseline Compilation

As JIT compilation improved, V8's interpreter began to seem slow by comparison. The team introduced Liftoff, a baseline compiler that bridged the gap between interpreter and optimizing compiler.

Liftoff generates moderately optimized machine code quickly for warm-up execution. This machine code runs faster than the interpreter to boost startup, but doesn't take as long to compile as TurboFan.

Liftoff gave V8 a performance boost during app warm-up and reduced the need for costly TurboFan optimizations in less performance-critical code.

Ignition Interpreter: Faster and More Efficient

In 2018, V8 replaced its original interpreter with Ignition, a new interpreter designed to start up faster while doing less work.

Ignition improves on the old interpreter by:

  • Generating lean bytecode: Ignition outputs simpler, faster-to-execute bytecode.
  • Avoiding full AST construction: It doesn't generate a full abstract syntax tree.
  • Interpreter and compiler sharing AST: Some AST construction is shared between interpreter and compiler.

Together, these design changes reduced V8 interpreter overhead by 5X-10X. This allowed JavaScript to start up and run more efficiently.

V8 Interpreter and Compiler Pipeline

Here's a diagram summarizing V8's interpreter and compiler pipeline:

JavaScript Code 
Ignition Interpreter 
Liftoff Baseline Compiler
TurboFan Optimizing Compiler
Optimized Machine Code
  1. Ignition Interpreter parses and profiles code.
  2. Liftoff Compiler generates moderately optimized code quickly.
  3. TurboFan applies advanced optimizations for hot code paths.

This tiered pipeline maximizes performance across a spectrum from startup to steady state optimization.

Concurrent Compilation: Smooth Sailing Through Code

As amazing as JIT compilation was, it did have one downside - it took time! Code could pause while waiting for hot path compilations to finish.

To combat this, V8 introduced concurrent compilation using background threads. Now JavaScript execution proceeds smoothly and doesn't wait for compilations to complete.

This concurrency ensured buttery smooth web app performance. V8 was thoughtful enough to not keep us waiting!

Memory Management: Cleaning Up Efficiently

To sustain great performance across long-running apps, V8 had to handle memory well. It did this using generational garbage collection.

V8 divides memory into spaces for new and old objects. It quickly reclaims newly allocated objects in nursery spaces since they often go out of use fast. Long-lived objects get promoted to old space.

By reclaiming short-lived objects aggressively, V8 ensured long, smooth-running JavaScript applications.

WebAssembly: Near-Native Speeds in the Browser

Even with all these optimizations, some code is just better suited to native compilation, like 3D games. That's why V8 adopted WebAssembly - a low-level binary format that runs at near-native speed.

Developers can compile C/C++ code to WebAssembly and run it in the browser alongside JavaScript. V8 compiles and executes WebAssembly just like JavaScript but with minimal overhead.

This allows CPU-intensive applications to reach new levels of performance in the browser by integrating natively compiled WebAssembly modules. Talk about versatility!

V8 on Mobile: Optimizing for Constraints

Running V8 on memory- and power-constrained mobile devices posed new challenges. The team optimized V8 for mobile through features like:

  • Code caching: Stores previously compiled code to minimize recompilation.
  • Memory reduction: Limits memory usage for compiler optimization data.
  • Battery optimizations: Minimizes CPU usage to preserve battery life.

These mobile-focused improvements ensured V8 could deliver great performance not just on desktops, but all the way down to low-power phones and embedded devices.

V8 on Servers: Optimizing for Scale

V8 serves as the engine for Node.js, powering huge server-side applications. Running at scale posed optimization challenges like:

  • Startup latency: V8 had to minimize startup time so servers can handle requests quickly.
  • Memory usage: Server apps tend to use large codebases and stay running a long time. V8 had to minimize memory consumption.
  • Throughput: Servers must handle high request throughput and response times.

To optimize for these server needs, V8 introduced:

  • Code caching and lazy compilation to reduce startup latency.
  • Efficient garbage collection and memory segmentation to lower memory usage.
  • Compiler optimizations like inlining, concurrency, and optimization sampling to maximize throughput.

Thanks to server-targeted optimizations, V8 enabled JavaScript to power backends for companies like PayPal, Netflix, and LinkedIn.

Orinoco: Leveraging GPU Parallelism

V8's compilers generate optimized sequential machine code. But modern GPUs have thousands of cores for parallel processing.

The Orinoco project aims to leverage GPU parallelism by adapting V8's compilers to generate parallel code that runs efficiently across GPU cores.

This unlocks huge performance potential for parallelizable JavaScript workloads like machine learning and image processing. Orinoco is an experimental project but shows promise for the future of parallel JavaScript execution.

The Future of JavaScript Performance

V8's innovation over the past decade has been nothing short of remarkable. What started as a pokey scripting language is now a world-class high-performance platform.

And the future looks even brighter! The V8 team continues finding new ways to optimize JavaScript for emerging use cases like WebAssembly, WebGPU, and parallel execution.

JavaScript performance has come incredibly far thanks to V8's groundbreaking work. With continued enhancement of engines like V8, JavaScript is sure to reach exciting new levels of speed in the years ahead. Buckle up, because this rocket ship still has momentum!

Subscribe to JS Dev Journal

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.