In this text, we are going to look into Node.js and V8. Node.js is an important a part of the JavaScript ecosystem, as it’s utilized in the backend to provide an entire software. It is usually thought of part of the well-liked MERN(MongoDB, ExpressJSReactJS and Node.js) stack and MEAN (MongoDB, ExpressJS, Angular and Node.js) stack. 

The V8 engine is what powers Node.js and it’s an open-source engine on which even Chrome works. It parses and runs your JavaScript inside a Node atmosphere. 

Overview on Node.js

Node.js was created by Ryan Dahl in 2009 and since then it has turn into very fashionable backend know-how. Till then the backend was dominated by languages like PHP, ASP.NET and Java. It has turn into well-liked as a result of it permits a Frontend developer with JavaScript abilities to simply create full stack apps.

The formal definition on the official Node.js web site describes Node.js as “a JavaScript runtime built on Chrome’s V8 JavaScript engine.

Node.js got here into existence when its creator Ryan Dahl, understanding the energy of V8, energyed the Chrome browser and prolonged it in order that it could run in your machine as a standalone software. 

Another a part of the definition on the official Node.js web site says,Node.js makes use of an occasion pushed, non-blocking I/O mannequin that makes it light-weight and environment friendly.

I/O refers to enter/output and that is the place the extra performance of Node.js comes into play. We can learn and edit native information in Node.js and additionally do an HTTP request to an API. 

The earlier backend programs like PHP and ASP used to dam the program until a community request was full. But it was fully modified by Node.JS, which sends the request and then goes to the subsequent line of code. So, it’s non-blocking and quicker than the earlier backend applied sciences. 

But it’s a single-threaded know-how and that’s the place it has some limitations, the placeas Java shines due to it being multi-threaded. 

Yet one other a part of the official definition on the Node.js web site says,Node.js package deal ecosystem, npm is the largest ecosystem of open-source libraries in the world. 

Over the previous decade, a tremendous group of open-source fans have created greater than 1 million npm packages, which improve the capabilities of Node.js.

It is totally opensupply and anybody can use it, because it has an MIT licence, for growing server-side and networking purposes. It can run on all three Operating Systems i.e., Mac OS, Windows, and Linux.

Overview on V8 JavaScript engine

V8 is Google’s open-source JavaScript engine, which is written in C++. It was developed in 2008 for Google Chrome and Chromium primarily based browsers (like Brave), however was used to construct Node.js for server-side coding. In reality, the V8 engine, can also be utilized by JSON primarily based No-SQL databases like Couchbase and the well-liked MongoDB. Besides this, V8 additionally powers the well-liked desktop software framework Electron and the newest server-side runtime atmosphere of Deno. 

V8 is JavaScript engine, as a result of it takes our JavaScript and executes it whereas shopping in Chrome. It truly supplies a runtime atmosphere wherein JavaScript executes. The wonderful thing about that is that the JavaScript engine is unbiased of the browser wherein it executes. This is the function that prompted the creator of Node.JS to decide on V8 engine to energy Node.JS and the relaxation is historical past. The recognition of Node.JS exploded and the V8 engine was additionally used to create desktop frameworks and databases.

There are different JavaScript engines like SpiderMonkey utilized by Firefox, and JavaScript Core utilized by Safari. Microsoft’s Edge was initially primarily based on Chakra JavaScript engine, however has been not too long ago re-built with Chromium and V8 engine.

How V8 Engine works 

A JavaScript Engine is an interpreter which executes JavaScript code. We can create JavaScript engine in two methods – the first manner is to implement as an ordinary interpreter which is completed by SpiderMonkey from Mozilla. The different manner is the Just-in-time (JIT) compilation, which converts the native JavaScript code to machine code and that’s the manner V8 makes use of it. So, the distinction between V8 code and others is that it doesn’t produce any intermediate code. 

When a developer or program runs a JavaScript on V8(i.e. in browser or Node atmosphere), the Ignition interpreter compiles the JavaScript code and generates non-optimized machine code. On runtime, the machine code is analyzed and re-compiled for greatest efficiency, by the Turbofan and Crankshaft parts of V8. 

The V8 engine additionally makes use of another parts, together with the ones we had seen above. They are Liftoff and Orinoco 

  • Liftoff is chargeable for machine code era in a extremely optimized manner. It generates code for every opcode and carry out manner higher then Turbofan.
  • Orinoco is chargeable for rubbish assortment. It appears for disconnected reminiscence allocations and carry out operations to release more room. It additionally replace the tips that could new reminiscence places.

V8 additionally makes use of quite a lot of completely different threads and they’re – 

  • The major thread fetches and compiles the JavaScript code.
  • There is one other thread which is used to optimize the working code, whereas the major thread continues its execution. 
  • Yet one other thread is used for profiling, which informs on runtime the strategies which are wanted to be optimized. 
  • Some of the threads additionally do rubbish assortment.

The Just-in-Time Paradigm

We will study a bit extra about the Just-in-Time (JIT) compilation in V8. For a code to execute in any programming language, it should be transformed into machine code, which the laptop understands. There is a completely different paradigm for this transformation. 

Most of the conventional languages created earlier than JavaScript like C++ and Java, carry out one thing referred to as Ahead-of-Time compilation. Here, the code is reworked into machine code earlier than the execution of our program throughout compile time. Anyone who has labored with Java or C++ is aware of that we run instructions like under to compile a Java or C++ program.

javac MyJavaProgram.java 
g++ -o mycppprogram mycppprogram.cpp 

This converts the code into machine code after which we are able to run our program with instructions like under.  

java MyJavaProgram 
./mycppprogram 

On the different hand, in languages like JavaScript and Python, every line of code is executed at runtime. This is completed as a result of it’s unattainable to know the actual code earlier than execution. In a browser, you by no means compile a code first and then run it, as a result of it’s accomplished robotically behind the scenes.

So, the Ahead-of-Time compilation produces extra optimized and quick code, due to the compilation accomplished earlier than hand. Which is why interpretation accomplished by languages like JavaScript are slower.

To overcome this drawback in dynamic languages, the method of Just-in-Time (JIT) compilation, was created, which mixes the better of each interpretation and compilation. So, an interpretation step runs earlier than the compilation step, the place the V8 engine detects the extra continuously used features and code and compiles them utilizing info from earlier executions.

During compile time, this code is re-compiled for optimum efficiency.

What is the relationship between Node and V8?

The Node.js is referred to as a runtime atmosphere, which incorporates every thing you have to run a program written in JavaScript.

The core powering Node.js is that this V8 engine. The diagram reveals a comparability with the Java Virtual Machine (JVM), which energy the Java Runtime atmosphere. Beside the V8 engine the Node.js runtime atmosphere provides many Node APIs to energy the Node.js atmosphere. We may also lengthen the performance of our node code by putting in extra npm packages.

One factor to grasp is that V8 is basically an unbiased C++ library, that’s utilized by Node or Chromium to run JavaScript code. V8 exposes an API that different code can use, so if in case you have your personal C++ program, you may embed V8 in it and run a JavaScript program. That is how it’s accomplished by Node and Chrome.

Suppose, we wish to add a performance in our JavaScript code to have statements like print(‘hello world’), along with the console.log(‘Hello World’). We can add our personal implementation of print operate in C++, in V8, which is anyway open sourced. 

Can Node.js work with out V8?

The present Node.js engine can’t work with out V8. It would don’t have any JavaScript engine and therefore no capability to run any JavaScript code. The reality is that the native code bindings, which include Node.js like the fs module and the Net module, depend on the V8 interface between C++ and JavaScript.  

Although in the tech world every thing is feasible and Microsoft in July 2016, made an effort to make use of Chakra JavaScript engine (which was utilized in Edge browser at the moment) in Node.js and change the V8 engine that challenge by no means took off and Microsoft Edge itself not too long ago moved to Chromium, which makes use of V8 JavaScript engine.

The new child on the block for server-side programming is DENO. Many think about that it might be a alternative to Node.js in the subsequent 2-3 years, and it additionally makes use of V8 JavaScript engine underneath its hood.

Summary 

We have acquired an summary of Node.js runtime atmosphere and V8 JavaScript engine on this put up. Then, we now have gone via the working of the V8 engine. We additionally investigated particulars of the Just-in-Time compilation, utilized by V8 JavaScript engine. Also, we now have understood the relationship between Node.js and V8 engine and how V8 engine is unbiased of Node.js.

Lastly, we now have learnt that it’s not doable for Node.js to run with no JavaScript engine like V8. It can, nevertheless, get replaced by one other JavaScript engine like Chakra from Microsoft; though that is extremely unbelievable,  it is nonetheless doable.