JavaScript V8 Engine in Node JS

JavaScript V8 Engine in Node JS

What is Node JS?

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, which was designed to build scalable network applications.

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.

V8 implements ECMAScript and WebAssembly, and runs on Windows 7 or later, macOS 10.12+, and Linux systems that use x64, IA-32, or ARM processors.

Working of V8 Engine?

There are basically three steps involved in processing the code:

Parsing the code

  • During the parsing phase, the code is broken down into its respective tokens.

Compiling the code

The compilation is the process of converting human-readable code to machine code. There are two ways to compile the code :

Using an Interpreter: The interpreter scans the code line by line and converts it into byte code. Example: Python

Using a Compiler: The Compiler scans the entire document and compiles it into highly optimized byte code. Example: Java

The V8 engine uses the Ignition interpreter, which takes in the Abstract Syntax Tree as the input and gives the byte code as the output, which further proceeds to the execution phase. When the code is being interpreted, the compiler tries to talk with the interpreter to optimize the code.

Executing the code

The byte code is executed by using the Memory heap and the Call Stack of the V8 engine’s runtime environment. The memory Heap is the place where all the variables and functions are assigned memory.

How to use the V8 modules

The Node.js V8 module represents interfaces and events specific to the version of V8. It provides methods to get information about heap memory through v8.getHeapStatistics() and v8.getHeapSpaceStatistics() methods.

To use this module, you need to use require('v8').

const v8 = require('v8');

v8.getHeapStatistics()

The v8.getHeapStatistics() method returns statistics about heap such as total heap size, used heap size, heap size limit, total available size etc.

Code

const v8 = require("v8");
console.log(v8.getHeapStatistics());

Output

  does_zap_garbage: 0,
  number_of_native_contexts: 1,
  number_of_detached_contexts: 0,
  total_global_handles_size: 8192,
  used_global_handles_size: 3040,
  external_memory: 325499

v8.getHeapSpaceStatistics()

The v8.getHeapSpaceStatistics() returns statistics about heap space. It returns an array of 5 objects: new space, old space, code space, map space and large object space. Each object contains information about space name, space size, space used size, space available size and physical space size.

Code

const v8 = require('v8');  
console.log(v8.getHeapSpaceStatistics());

Output

[
  {
    space_name: 'read_only_space',
    space_size: 176128,
    space_used_size: 170944,
    space_available_size: 0,
    physical_space_size: 176128
  },
  {
    space_name: 'old_space',
    space_size: 3137536,
    space_used_size: 3080880,
    space_available_size: 192,
    physical_space_size: 3137536
  },
  {
    space_name: 'code_space',
    space_size: 368640,
    space_used_size: 333056,
    space_available_size: 0,
    physical_space_size: 368640
  },
  {
    space_name: 'map_space',
    space_size: 270336,
    space_used_size: 257912,
    space_available_size: 0,
    physical_space_size: 270336
  },
  {
    space_name: 'large_object_space',
    space_size: 0,
    space_used_size: 0,
    space_available_size: 0,
    physical_space_size: 0
  },
  {
    space_name: 'code_large_object_space',
    space_size: 0,
    space_used_size: 0,
    space_available_size: 0,
    physical_space_size: 0
  },
  {
    space_name: 'new_large_object_space',
    space_size: 0,
    space_used_size: 0,
    space_available_size: 1031072,
    physical_space_size: 0
  },
  {
    space_name: 'new_space',
    space_size: 1048576,
    space_used_size: 353952,
    space_available_size: 677120,
    physical_space_size: 1048576
  }
]

Hope you have learned something about JavaScript V8 Engine🀩🎊

Will come up with some new topics, till then Keep Learning, Keep Exploring!

Β