V8-JS Engine

V8-JS Engine

ยท

4 min read

History of v8

  • v8 is a open source JavaScript engine
  • It was developed by Google in 2008 for Google Chrome and Chromium-based browsers
  • It is written in C++.
  • It translates our JavaScript source code into machine instructions.
  • It actually provides a runtime environment in which JavaScript executes.
  • The JavaScript engine is independent of the browser in which it executes.

v8.png

Relation between Node.js and V8

Node.js is referred to as a runtime environment, which includes everything you need to run a program written in JavaScript. V8 is the core thing that powers Node.js. Beside the V8 engine the Node.js runtime environment adds many Node APIs to power the Node.js environment.

node & v8.png V8 is an independentC++ library. It exposes an API that other code can use. If we have C++ program, we can embed V8 in it and run a JavaScript program. Currently node.js can't work without v8. The native bindings of node.js rely on the V8 interface between C++ and JavaScript.

v8 JS engine is used ,

๐Ÿ‘‰ to build Node.js for server-side coding.

๐Ÿ‘‰ to create desktop framework Electron.

๐Ÿ‘‰ by JSON based No-SQL databases like Couchbase and MongoDB.

๐Ÿ‘‰ to power Deno, the latest server-side runtime environment.

A JavaScript Engine is an interpreter which executes JavaScript code. There are 2 ways to create JS engine.

โ˜ Implement as a standard interpreter.

โœŒ Just-in-time (JIT) compilation (which converts JS code to machine code)

V8 uses JIT compilation.

Methods in V8

The node:v8 module exposes APIs that are specific to the version of V8 built into the Node.js binary. It can be accessed( importing v8 module) using:

const v8 = require('node:v8')
// or
const v8 = require('v8')
// Both node:v8 , v8 are same

v8.cachedDataVersionTag()

  • Used to get a version tag derived from the V8 version, command-line flags, and detected CPU features.
  • Returns an integer.
  • The data carried by CachedData may depend on the exact V8 version number or current compiler flags.

Syntax

v8.cachedDataVersionTag()

Example

// import v8 module
const v8 = require('v8');

console.log(v8.cachedDataVersionTag()); // Ans : 844655377

// when flags are toggled
v8.setFlagsFromString('--allow_natives_syntax');
console.log(v8.cachedDataVersionTag()); // Ans 2256857713

v8.setFlagsFromString(flags)

  • This method is used to set the command line flags for v8 in a programmatic way.
  • In this method flags should be string.
  • We have to be cautious, when we use this method.
  • Any uncertainty may lead to data lose, crashes or it may do nothing.

Syntax

v8.setFlagsFromString(flags)

Example

const v8 = require('v8');

// set command line flag
// flag should be string.
v8.setFlagsFromString('--harmony'); //To fix the regex Lookbehind issue

Some of the flags in v8

  • --use_strict (enforce strict mode)
  • --expose_gc (expose gc extension)
  • --trace_gc (print one trace line following each garbage collection)

v8.getHeapCodeStatistics()

  • Used to get statistics about code and its metadata in the heap.
  • Returns an object.
  • The object contains some properties which holds value in the form of number.

Syntax

v8.getHeapCodeStatistics()

Example

console.log(v8.getHeapCodeStatistics()); 

// Output
{
  code_and_metadata_size: 130888,
  bytecode_and_metadata_size: 111032,
  external_script_source_size: 298712
}

v8.getHeapStatistics()

  • Used to get statistics about heap derived from the v8 version.
  • Returns object.
  • All properties in the returned object hold values as numbers.

Syntax

v8.getHeapStatistics()

console.log(v8.getHeapStatistics());

// Output
{
  total_heap_size: 4931584,
  total_heap_size_executable: 524288,
  total_physical_size: 4931584,
  total_available_size: 2194643104,
  used_heap_size: 4077120,
  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
}

v8.serialize(value)

  • Used to serialize JavaScript values in a way that is compatible with the HTML structured clone algorithm. The format is backward-compatible also.
  • This method needs one parameter (value).
  • Value may be any type of data like number, string etc.
  • It returns buffer.
  • To serialize value into a buffer, DefaultSerializer is used.

Syntax

v8.serialize(value)

Example

// value is string
console.log(v8.serialize('serialize'));
// output :<Buffer ff 0d 22 09 73 65 72 69 61 6c 69 7a 65>

//value is number
console.log(v8.serialize(4532));
//output :<Buffer ff 0d 49 e8 46>

v8.deserialize(buffer)

  • Used to deserialize a buffered data into JS value.
  • It needs buffer as parameter.
  • Returns JS value.
  • `DefaultDeserializer is used for deserializing.

Syntax

v8.deserialize(buffer)

Example

console.log(v8.deserialize(v8.serialize('serialize')));
// output : serialize

console.log(v8.deserialize(v8.serialize(4532)));
// output : 4532

Along with the v8 methods we have seen above , There are more methods available in v8. You can refer them here ๐Ÿ‘‰ v8|Node js.

Thank You for reading. ๐Ÿค—๐Ÿค—๐Ÿค—

ย