Edge Academy

Definition

V8 JavaScript engine is an open source JavaScript and WebAssembly engine that compiles JavaScript to optimized machine code before execution.

Overview

V8 JavaScript engine was initially developed for Google Chrome and Chromium web browsers to improve the performance of JavaScript execution. The project’s creator, Lars Bak, created the first version that was released at the same time as the first version of Google Chrome in September 2008. The engine was initially designed solely for execution by web browsers, but the latest versions also execute JS code outside the browser, enabling server-side scripting.

To achieve faster JavaScript execution speeds, V8 translates JS code to more efficient machine code instead of using an interpreter. Most modern JavaScript engines like SpiderMonkey or Rhino follow the same approach, but what makes V8 stand out is that it does not produce any intermediate code.

How V8 JavaScript Engine Works

When a developer runs a JS script on V8, the following steps are taken by the engine:

  1. The engine compiles and executes the JS code
  2. The engine handles the call stack
  3. The engine manages the memory heap
  4. The engine handles the garbage collection
  5. The engine provides all the data types, objects and functions
  6. The engine also provides the event loop (sometimes implemented by the browser as well)

Just in time compilation

The V8 engine gets its speed from the Just in Time (JIT) compilation of JS code to native machine code. The ignition interpreter, a key component of V8, compiles the JS code and generates non-optimized machine code. On runtime, the machine code is analyzed and re-compiled for optimal performance. This optimization is handled by the TurboFan and Crankshaft components of V8.

Other important components

Alongside the above-mentioned components, V8 engine has a few other important components, namely Liftoff and Orinoco.

  • LIftoff is responsible for machine code generation and performs its tasks in a highly optimized fashion. It’s a simpler code generator that generates code for each opcode and performs ten times better than TurboFan.
  • Orinoco is responsible for garbage collection. Its job is to look for disconnected memory allocations and perform evacuation and compaction to free up space for more objects. Orinoco also updates pointers to new memory locations and marks the living objects in black to speed up the iterative marking process.

Example of V8 JavaScript Engine

StackPath’s serverless scripting product is built on the V8 engine, making it easier for developers to build and deploy scripts at the Internet’s edge. The scripting engine comes with WebAssembly support that enables support for a number of other languages.

Serverless scripts delivered at the edge are considerably faster than scripts delivered with cloud-based services such as AWS Lambda that has a 800ms start time. In comparison, StackPath’s scripting engine starts in 50ms and is deployed across all edge locations.

Here’s an example of a script deployed on StackPath’s serverless scripting engine sandbox:

addEventListener("fetch", event => {
 event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
 try {
 let region = request.headers.get("x-sp-server-region");
 console.log("You are in "+region);
 let response = await fetch(request);
 return response;
 } catch (e) {
 return new Response(e.stack || e, { status: 500 });
 }
}

The script lets developers identify the server from which the request is being made. The developer can then use this information to provide region-specific services in real time.

Key Takeaways

  • V8 engine is the backbone of Google Chrome and other Chromium-based web browsers.
  • V8 engine is distinct from other JS engines as it directly converts scripts to machine code without producing intermediate code.
  • Edge service providers like StackPath use the V8 engine to provide better serverless scripting services.
Related Products
Request Demo

Have questions? Chat now with the live chat button on this page or request a demo from our edge experts.

Request a Demo
Get an Advantage

Stay informed of the latest edge news, updates and solutions.

Subscribe