Javascript V8 Engine in NodeJs

Javascript V8 Engine in NodeJs

Hello Everybody, In this blog, you will find out the concept behind Google's Javascript V8 Engine and it's working with Nodejs. Let's get walk through each fundamental step by step.

What is Node Js actually mean?

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on a JavaScript Engine and executes JavaScript code outside a web browser.

Its definition is as simple as it is, as you can see on its official docs page. Here All we are talking about Node.Js because it is built on Chrome’s V8 JavaScript engine.

Ryan Dahl who is the creator of NodeJs which truly understands the power of V8, powered the Chrome browser and extended it so that it can run on your machine as a standalone application.

It is completely open-source and anyone can use it, as it has an MIT license, for developing server-side and networking applications. It can run on all three Operating Systems i.e., Mac OS, Windows, and Linux.

What is V8 Engine?

V8 is Google's open-source high-performance JavaScript and WebAssembly engine, written in C++. It was developed in 2008 for Google Chrome and Chromium-based browsers (like Brave) but was used to build Node.js for server-side coding. In fact, the V8 engine is also used by JSON-based No-SQL databases like Couchbase and the popular MongoDB database.

As we know V8 is a JavaScript engine because it takes our JavaScript and executes it while browsing in Chrome. It actually provides a runtime environment in which JavaScript executes. The great thing about this is that the JavaScript engine is independent of the browser in which it executes. The popularity of Node.JS exploded and the V8 engine was also used to create desktop frameworks and databases.

How V8 Engine Works?

A JavaScript Engine is an interpreter which executes JavaScript code. We can create a JavaScript engine in two ways – the first way is to implement it as a standard interpreter which is done by SpiderMonkey from Mozilla. The other way is the Just-in-time (JIT) compilation, which converts the native JavaScript code to machine code and that is the way V8 uses it. So, the difference between the V8 code and others is that it does not produce any intermediate code.

how to use the V8 modules

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');

Let me walk through some of the V8 node modules in node js to get our desired output.

1) v8.cachedDataVersionTag()

It is not as complex as it seems, is simply a function that returns an integer representing a version tag derived from the V8 version, command-line flags, and detected CPU features.

snippet.png

2) v8.getHeapSnapshot()

Returns: A Readable Stream containing the V8 heap snapshot Generates a snapshot of the current V8 heap and returns a Readable Stream that may be used to read the JSON serialized representation.

This JSON stream format is intended to be used with tools such as Chrome DevTools. The JSON schema is undocumented and specific to the V8 engine. Therefore, the schema may change from one version of V8 to the next.

snippet (2).png

Likewise, there are so many functions that you can use from here.... I hope you get a clear idea about what we want to say. #HappyReading...