V8 NodeJs Basics

What is V8 ?

  • V8 is an open-source JavaScript engine developed by the Chromium project for the Google Chrome web browser. It is written in C++.
  • Nowadays, it is used in many projects such as NodeJS and MongoDB.

What is NodeJs?

  • NodeJS is an open-source development platform for executing JavaScript code server-side.
  • In simple words, we can use JavaScript outside Browser.

What is Heap in NodeJS?

  • The heap is a memory segment used for storing objects, strings, and closures. This is where all the magic happens.

cachedDataVersionTag():

  • The v8.cachedDataVersionTag() method is an inbuilt application programming interface of the v8 module which is used to get the version tag derived from the v8 version.
    const v8 = require("node:v8");
    console.log(v8.cachedDataVersionTag());     // 230789238

getHeapCodeStatistics():

  • Get statistics about code and its metadata in the heap.
    const v8 = require("node:v8");
    console.log(v8.getHeapCodeStatistics());
    /* Outout:
    {
        code_and_metadata_size: 134424,
        bytecode_and_metadata_size: 93656,
        external_script_source_size: 209763
    }
    */
  • This method returns an object that contains statistics about version 8 heap. The returned object usually contains an array, that consists of the following fields:
    • code_and_metadata_size, return number
    • bytecode_and_metadata_size, return number
    • external_script_source_size, return number

v8.getHeapStatistics():

  • The v8.getHeapStatistics() method is an inbuilt application programming interface of the v8 module which is used to get statistics about the heap derived from the v8 version.
    const v8 = require("node:v8");
    console.log(v8.getHeapStatistics());
    /* Output:
    {
        total_heap_size: 4866048,
        total_heap_size_executable: 524288,
        total_physical_size: 4866048,
        total_available_size: 2194755616,
        used_heap_size: 3965944,
        heap_size_limit: 2197815296,
        malloced_memory: 254064,
        peak_malloced_memory: 100384,
        does_zap_garbage: 0,
        number_of_native_contexts: 1,
        number_of_detached_contexts: 0
    }
    */
  • total_heap_size: Number of bytes V8 has allocated for the heap, This can grow if usedHeap needs more.
  • total_heap_size_executable: Number of bytes for compiled bytecode and JITed code.
  • total_physical_size: Committed size.
  • total_available_size: total available size.
  • used_heap_size: Number of bytes used by application data.
  • heap_size_limit: The absolute limit the heap cannot exceed.

v8.serialize():

  • The v8.serialize() method provides a method to serialize JavaScript values in a way that is compatible and therefore can be used with HTML structured clone algorithm. The format is also backward compatible.

  • HTML structured clone algorithm: The structured clone algorithm copies complex JavaScript objects, and it clones by recursing through the input object while maintaining a map of previously visited references, to avoid infinitely traversing cycles.

  • Serialization: The process whereby an object or data structure is translated into a format suitable for transfer over a network, or storage.

  • This Method takes a single parameter which takes any type of value and returns a buffer output after serializing the input value.

    const v8 = require("v8");
    var data = "Welcome to HashNode!";
    console.log(v8.serialize(data));
    // Output:
    // <Buffer ff 0d 22 14 57 65 6c 63 6f 6d 65 20 74 6f 20 48 61 73 68 4e 6f 64 65 21>

Hope you like this basic V8 NodeJs article.

#JavaScript

#NodeJs

#learncodeonline

#iwritecode