Node V8 Module

Node V8 Module

Node.js is a JavaScript runtime that is built on the V8 JavaScript engine. Node.js needs a v8 engine for execution.

Node: V8

The node: v8 module exposes or shows APIs that are only specific to the version of V8 that is built into the Node.js binary. You can access using :

const v8 = require("node: v8")

Node.js v8.getHeapStatistics()

  • It returns the object.

Following properties are returned as an object

  • total heap size in a number type.
  • total heap size executable in a number type.
  • total physical size in a number type.
  • total available size in a number type.
  • used heap size in a number type.
  • heap size limit in a number type.
  • external memory in a number type.
  • used global handles size and total global handles in a number type.
{
  total_heap_size: 7326976,
  total_heap_size_executable: 4194304,
  total_physical_size: 7326976,
  total_available_size: 1152656,
  used_heap_size: 3476208,
  heap_size_limit: 1535115264,
  malloced_memory: 16384,
  peak_malloced_memory: 1127496,
  does_zap_garbage: 0,
  number_of_native_contexts: 1,
  number_of_detached_contexts: 0,
  total_global_handles_size: 8192,
  used_global_handles_size: 3296,
  external_memory: 318824
}

v8.serialize(value)

The v8.serialize() method is an inbuilt API of the v8 module which serialize any type of data into a buffer.

  • Here, value is accepted as parameter where as value can be any type.

SYNTAX

v8.serialize(value)
 console.log(v8.serialize("hello"))

Output :

<Buffer ff 0d 22 0d 67 65 65 6b 73 66 6f 72 67 65 65 6b 73>

v8.getHeapSnapshot()

It generates a snapshot of the current V8 heap and returns a readable Stream that may be used to read the JSON serialized representation. The JSON stream format is intended to be used with tools such as Chrome DevTools..

const stream = v8.getHeapSnapshot();
stream.pipe(process.stdout);