Updated April 14, 2023
Introduction to Node.js V8
Nodejs is cross platform, open source javascript run time environment. nodejs v8 is a javaScript engine. Nodejs has been developed on v8 engine. Nodejs has v8 module. This module exposes APIs that are build with nodejs binary. V8 is an open source JavaScript engine. Basically it is written in JavaScript. This is useful in various ways. We have one module called v8 in nodejs. This mpdule in nodejs focuses on events and interfaces. This module compromises various methods. As other modules we need to import v8 in node js to use it. There are many technologies which make use of v8 module. We will see this module in detail.
Syntax
To use this module in our project, we need to import it. And as we know we can import the module in node js with require method. Look at the below syntax for more details.
const v8 = require('v8');
Above syntax helps us to import v8 module in node js program.
How V8 works in Node.js?
As we already seen the v8 module helps us with events and interfaces. Also we have methods to get information about memory. In this case we are talking about heap memory. For using any method in v8, we need to import that module in nodejs with the syntax shown above.
V8 has some memory limitations as per the system. Like on 32-bit it has of 512mb. And 1gb on 64-bit. We can increase the memory limit as per requirements. We can maximize the limit with following command.
setting --max-old-space-size
- We can increase upto 1gb for 32-bit and 1.7 for 64-bit. The current binary of nodejs does not work without v8.
- Nodejs has used google chrome browsers v8 engine. There are many releases of this version. V8 version 5 and the versions before that had two compilers. The chrome engine has used c++ while implementing its engine. V8 is bvery important factor while working with the node js.
- V8 is a javascript engine. This javascript engine is used to interpret javascript code into the machine code. There are many different engines that exist depeinding upon the browser. like V8 for chrome, Chakra for IE, Spider monkey for Netscape etc.
Node.js V8 Methods
We have the following two methods for heap memory in nodejs.
1. getHeapStatistics()
The getHeapStatistics() function returns all the details about heap memory. Look at following ex:
Code:
const v8engine = require('v8');
console.log(v8engine.getHeapStatistics());
Output:
2. getHeapSpaceStatistics()
This is the method used to know the details about the space in heap. This method gives the information in the form of an array of objects. Each object gives exact details about different values like name, space size, availability os space, physical space size, space used size etc. We will look at this in detail with the following example.
Code:
const v8engine = require('v8');
console.log(v8engine.getHeapSpaceStatistics());
Output:
There are some other methods also. One of them is v8.getHeapSnapshot().
1. v8.getHeapSnapshot(): This method returns the readable stream of v8 heap snapshot. This method generates the snapshot of current heap and returns the readable stream to use JSON data representation.This snapshot has json format which is irrespective of version to version.
Code:
const v8StreamData = v8.getHeapSnapshot();
v8StreamData.pipe(process.stdout);
This command gives back the long stream of data. So cant show here in a screen shot. Please type above command in node js command prompt and see the output. Now, here the time is to see one more function about v8 module in node js.
2. v8.setFlagsFromString(flags): Every method does its significant role, now let’s look at this method. This method sets the command line flags for v8 in a programmatic way. While using this method we need to be cautious. Any uncertainty may lead to data losses. IT may behave unpredictable or crash the system. Also it be doing nothing. Look at the following example. We will use above function to generate events.
Code:
const v8engine = require('v8');
v8engine.setFlagsFromString('--trace_gc');
setTimeout(() => { v8engine.setFlagsFromString('--notrace_gc'); }, 60e3);
Above method has generated the events. Try it in nodejs console.
3. writeHeapSnapshot([filename]): we have one more method to deal with node js v8 module. And that method is v8.writeHeapSnapshot([filename]). This method is used to find the snapshot of the heap. With the following pattern snapshot of the v8 heap is saved.
Code:
filename<string>
if not specified then, filename with some pattern got generated which had PID and thread id. And then writeHeapSnapshot will be called from node js work thread id.
This all process will return the filename in string format. This generates the snapshot and write to the json file. We mostly see this file is getting used in browsers devtool like we have in chrom browser. The various version of v8 module has different file.it varies from version to version.
Conclusion
Node js v8 is the module in nodejs. Also nodejs is itself made top of it. V8 engine in chrom browser has built upon c++. Every browser has its own engine. Same like chrome browser has v8,Chakra for IE, Spider monkey for Netscape etc..need to understand the working of nodejs v8 module. Try to practice this module, it is useful in various aspects of programming.
Recommended Articles
This is a guide to Node.js V8. Here we discuss an introduction to Node.js V8, syntax, how does it work with programming examples. You can also go through our other related articles to learn more –