Inside the V8 Engine: How Google is Transforming JavaScript Performance

Arthur Kaminsky
CodePubCast
Published in
5 min readFeb 1, 2023

Exploring the Benefits of Compilation and Just-In-Time Optimization for Modern Web Applications

Photo by Growtika on Unsplash

JavaScript is one of the most widely used programming languages in the world, powering everything from simple websites to complex server-side applications. However, as applications have become more complex and demanding, the need for a high-performance JavaScript engine has become more pronounced. That’s where the V8 engine comes in.

Developed by Google, the V8 engine is an open-source JavaScript engine that is designed to be fast, efficient, and scalable. The engine is used to power many popular applications, including the Chrome browser and Node.js, and it has become one of the most important tools in the modern web developer’s toolkit.

One of the key features of the V8 engine is its ability to compile JavaScript code into machine code, rather than interpreting it line by line. This results in much faster code execution, as the compiled code can be executed directly by the CPU.

Here’s a simple code example that demonstrates the difference between interpreted code and compiled code:

// Interpreted code example
function add(a, b) {
return a + b;
}

// Compiled code example
function add(a, b) {
var result = a + b;
return result;
}

As you can see, the compiled code is more efficient, as it stores the result of the calculation in a variable before returning it. This might seem like a minor optimization, but when you’re working with complex applications that contain thousands of lines of code, every little optimization can make a big difference in performance.

In addition to its compilation capabilities, the V8 engine also has a Just-In-Time (JIT) compiler that compiles frequently used code into machine code. This is an extremely powerful feature, as it allows the engine to optimize the performance of your code in real-time, by identifying the most frequently used code and compiling it into machine code on-the-fly.

In order to verify the code snippets above, you can create a test.js file and paste the following code inside the file:

// Interpreted code example
function add1(a, b) {
return a + b;
}

// Compiled code example
function add2(a, b) {
const result = a + b;
return result;
}

console.time("add1");
console.log(add1(5, 5));
console.timeEnd("add1");

console.time("add2");
console.log(add1(5, 5));
console.timeEnd("add2");

The result for me was the following:

Storing the result of a calculation in a variable before returning it can be more efficient because it allows the calculation to be performed only once, rather than every time the result is needed. This is known as memoization, which is a technique used to optimize the performance of repeated function calls.

By storing the result of the calculation in a variable, you can avoid recalculating the result each time the function is called with the same input. This can lead to a significant performance improvement, especially when the calculation is complex or time-consuming.

In addition, for more complex Nodejs projects, it’s more preferable to store the result of an operation inside a variable before returning it, so you can debug better before returning and improve performance.

NodeJs Web Application

Now that we’ve talked a bit about V8 theoretically and providing a small example of it’s strength in terms of performance, let’s dive into more interesting topics such as developing APIs using Nodejs.

Note: Node.js runs the code using the V8 JavaScript engine which compiles JavaScript into machine code and runs it. The compilation process happens automatically and behind the scenes.

Here’s a simple example of a REST API written using the Koa framework that manages storage and uses a PostgreSQL database:

const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const { Client } = require('pg');

const app = new Koa();
const router = new Router();

const client = new Client({
connectionString: process.env.DATABASE_URL,
});

client.connect();

router.post('/storage', async ctx => {
const { body } = ctx.request;
const { data } = body;

const query = `
INSERT INTO storage (data)
VALUES ($1)
RETURNING *
`;

const values = [data];

const { rows } = await client.query(query, values);
const [created] = rows;

ctx.body = created;
});

router.get('/storage/:id', async ctx => {
const { id } = ctx.params;

const query = `
SELECT *
FROM storage
WHERE id = $1
`;

const values = [id];

const { rows } = await client.query(query, values);
const [found] = rows;

ctx.body = found;
});

app
.use(bodyParser())
.use(router.routes())
.use(router.allowedMethods());

app.listen(3000);

This code creates a REST API with two endpoints:

  • POST /storage - creates a new storage entry with the data sent in the request body
  • GET /storage/:id - retrieves a storage entry by id

It uses the Koa framework to handle HTTP requests, the Koa Router to define routes, and the Koa body parser to parse request bodies. The PostgreSQL database is connected using the pg library.

In conclusion, the V8 engine is transforming the way that JavaScript is executed, providing developers with a fast, efficient, and scalable solution for running their applications. With its compilation and JIT optimization capabilities, the V8 engine can deliver substantial performance improvements over traditional JavaScript engines, and its support for modern JavaScript features makes it an ideal choice for today’s web applications.

If you’re interested in learning more about the V8 engine and how it can benefit your applications, there are many resources available online, including the V8 engine documentation and the V8 engine source code. Additionally, you can find a wealth of tutorials, articles, and discussion forums dedicated to the V8 engine, which can help you to get up to speed quickly and start using this powerful tool to transform your applications.

So if you’re looking to take your JavaScript performance to the next level, consider using the V8 engine and see how it can transform the way that you write and execute your code. Whether you’re building simple websites or complex applications, the V8 engine is an excellent choice for developers who want to deliver high-performance and scalable solutions.

Thanks for reading thus far, and I hope the topic was interesting for you to know a bit on how V8 engine for JavaScript performance works.

Make sure to follow me and our publication “CodePubCast” for more programming content.

--

--

Arthur Kaminsky
CodePubCast

Constantly trying to pursue understanding of thyself and others.