• Node.js Video Tutorials

Node.js - V8 Module



The chrome V8 engine is a high-performance JavaScript engine that is written in C++ and used in Google Chrome. The computer can only understand the machine language but not JavaScript or HTML objects.

The code written in JavaScript is understood only by the browsers but not by the machines. So, the JS V8 engine will convert/transform the JavaScript code into machine-understandable language so that it can be understood by the machine.

The NodeJS V8 module provides APIs that are specific to the version of V8 built into the Node.js binary.

Including the V8 module

To include the V8 module, add the below-given syntax at the beginning of your Node.js document.

const v8 = require('v8')

The V8 module provides us with methods to get the information about the heap memory using the NodeJS v8.getHeapStatistics() method and v8.getHeapSpaceStatistics() method. Let's look into them with suitable examples.

Printing statistics of the V8 heap

To print the statistics of the V8 heap, we use the NodeJS v8.getHeapStatistics() method.

Example

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

Output

{
   total_heap_size: 5369856,
   total_heap_size_executable: 524288,
   total_physical_size: 4303400,
   total_available_size: 17226376544,
   used_heap_size: 2849672,
   heap_size_limit: 17230200832,
   malloced_memory: 8192,
   peak_malloced_memory: 418904,
   does_zap_garbage: 0,
   number_of_native_contexts: 1,
   number_of_detached_contexts: 0
}

Printing statistics of the V8 heap space

To get the statistics of the V8 heap space, we use the NodeJS v8.getHeapSpaceStatistics() method.

Example

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

Output

[
   {
      space_name: 'read_only_space',
      space_size: 262144,
      space_used_size: 32808,
      space_available_size: 0,
      physical_space_size: 33088
   },
   {
      space_name: 'new_space',
      space_size: 2097152,
      space_used_size: 649416,
      space_available_size: 398040,
      physical_space_size: 2088040
   },
   { 
      space_name: 'old_space',
      space_size: 1916928,
      space_used_size: 1582320,
      space_available_size: 88312,
      physical_space_size: 1584560
   },
   {
      space_name: 'code_space',
      space_size: 430080,
      space_used_size: 155616,
      space_available_size: 0,
      physical_space_size: 172000
   },
   {
      space_name: 'map_space',
      space_size: 528384,
      space_used_size: 308880,
      space_available_size: 0,
      physical_space_size: 309720
   },
   {
      space_name: 'large_object_space',
      space_size: 135168,
      space_used_size: 131112,
      space_available_size: 0,
      physical_space_size: 135168
   },
   {
      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: 1047456,
      physical_space_size: 0
   }
]

V8 Module Methods

Following is a list of methods available in the V8 module

Sr.No Module & Description
1

cachedDataVersionTag()

Used to retrieve an integer value that will represent the version tag of the v8 version, command-line flags, and the detected CPU features.

2

getHeapSpaceStatistics()

Used to retrieve the statistics of the v8 heap space.

3

getHeapStatistics()

Used to retrieve the statistics about the heap that is derived from the v8 version

Serialization API Methods

Following is a list of methods available in the Serialization API

Sr.No Module & Description
1

serialize()

Used to serialize any data type into a buffer

2

deserialize()

Used to deserialize the buffer value into a JavaScript value.

Serializer Methods

Following is a list of methods available in the v8.Serializer class

Sr.No Module & Description
1

writeHeader()

Used for writing the header to the internal buffer.

2

writeValue()

Used to serialize a JavaScript value and adds the serialized representation to the internal buffer

3

releaseBuffer()

Used for retrieving the value stored in the internal buffer.

4

writeUnit32()

Used to write a raw 32-bit unsigned integer to the internal buffer.

5

writeUnit64()

Used to write a raw 64-bit unsigned integer to the internal buffer.

6

writeDouble()

Used to write a JavaScript number value to the internal buffer.

7

writeRawBytes()

Used to write raw bytes into the serializer's internal buffer.

Deserializer Methods

Following is a list of methods available in the v8.Deserializer class −

Sr.No Module & Description
1

readHeader()

Used to read and validate the header in the internal buffer.

2

readValue()

Used for deserializing the JavaScript value that is written in the internal buffer.

3

readUnit32()

Used to read a raw 32-bit unsigned integer and return it.

4

readUnit64()

Used to read a raw 64-bit unsigned integer and return it as an array with its higher and lower values.

5

readRawBytes()

Used to read raw bytes from the deserializer's internal buffer.

Advertisements