All about the JavaScript engine V8

All about the JavaScript engine V8

What is the V8 engine in Node.js?

V8 is a C++-based open-source JavaScript engine developed by Google. It was originally designed for Google Chrome and Chromium-based browsers (such as Brave) in 2008, but it was later utilized to create Node.js for server-side coding. In reality, JSON-based No-SQL databases like Couchbase and the widely used MongoDB use the V8 engine. V8 also powers Electron, a prominent desktop application framework, and Demo, the latest server-side runtime environment.

V8 is known to be a JavaScript engine because it takes JavaScript code and executes it while browsing in Chrome. It provides a runtime environment for the execution of JavaScript code. The best part is that the JavaScript engine is completely independent of the browser in which it runs. This is the feature that led Node.js designers to choose the V8 engine to power the framework, and the rest is history. The V8 engine was also utilized to construct desktop frameworks and databases as Node.JS grew in popularity.

How V8 works?

Whether JS is a compiled or interpreted language is not fixed. First of all, JS needs an engine to run, whether in a browser or in Node, which is a feature of interpreted languages.

However, under the V8 engine, the TurboFan compiler was introduced. It will optimize under certain circumstances and compile the code into a more efficient Machine Code. This compiler is not necessary for JS, but only to improve the code. Execution performance, so in general JS is more inclined to interpret languages.

In this process, the JS code is first parsed into an abstract syntax tree (AST) and then converted into Bytecode or Machine Code by an interpreter or compiler.

Let's see Image How V8 works:

V8

From the above figure, we can see that JS will be parsed into an abstract syntax tree(AST) first and then converted into Bytecode or Machine Code by an interpreter or compiler and then run optimized code!

How to use V8

Source Code: lib/v8.js

The node:v8 module exposes APIs that are specific to the version of V8 built into the Node.js binary. It can be accessed using:

const v8 = require('node:v8');

To use the v8 module we import it into our js file.

What is the relationship between Node and V8?

Node.js is referred to as a runtime environment, which includes everything you need to run a program written in JavaScript.

The core powering Node.js is this V8 engine. The diagram shows a comparison with the Java Virtual Machine (JVM), which powers the Java Runtime environment. Besides the V8 engine, the Node.js runtime environment adds many Node APIs to power the Node.js environment. We can also extend the functionality of our node code by installing additional npm packages.

comparision

One thing to understand is that V8 is essentially an independent C++ library, that is used by Node or Chromium to run JavaScript code. V8 exposes an API that other code can use, so if you have your own C++ program, you can embed V8 in it and run a JavaScript program. That is how it is done by Node and Chrome.

Suppose, we want to add a functionality in our JavaScript code to have statements like print(‘hello world’), in addition to the console.log(‘Hello World’). We can add our own implementation of the print function in C++, in V8, which is anyway open-sourced.

Can Node.js work without V8?

The current Node.js engine cannot work without V8. It would have no JavaScript engine and hence no ability to run any JavaScript code. The fact is that the native code bindings, which come with Node.js like the fs module and the Net module, rely on the V8 interface between C++ and JavaScript.

Although in the tech world everything is possible and Microsoft in July 2016, made an effort to use the Chakra JavaScript engine (which was used in Edge browser at that time) in Node.js and replace the V8 engine, that project never took off and Microsoft Edge itself recently moved to Chromium, which uses V8 JavaScript engine.

The new kid on the block for server-side programming is DENO. Many consider that it could be a replacement for Node.js in the next 2-3 years, and it also uses the V8 JavaScript engine under its hood.

Different JavaScript V8 engine method

  • v8.cachedDataVersionTag()

  • v8.getHeapCodeStatistics()

  • v8.getHeapSnapshot()

  • v8.getHeapSpaceStatistics()

  • v8.getHeapStatistics()

  • v8.setFlagsFromString(flags)

  • v8.stopCoverage()

  • v8.takeCoverage()

  • v8.writeHeapSnapshot([filename])

  • v8.setHeapSnapshotNearHeapLimit(limit)

And more

Other JS engines

Other browsers have their own JavaScript engine:

  1. Firefox has SpiderMonkey

  2. Safari has JavaScriptCore (also called Nitro)

  3. Edge was originally based on Chakra but has more recently been rebuilt using Chromium and the V8 engine.

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

And that’s it all about JavaScript engine V8. 🙂

I hope this reference guide has been helpful to you!

Thanks to:- Hitesh Chaudhary

mascot