For enquiries call:

Phone

+1-469-442-0620

April flash sale-mobile

HomeBlogWeb DevelopmentRelationship Between Node.Js and V8: A Complete Overview

Relationship Between Node.Js and V8: A Complete Overview

Published
29th Sep, 2023
Views
view count loader
Read it in
6 Mins
In this article
    Relationship Between Node.Js and V8: A Complete Overview

    In this article, we will look into Node.js and V8. Node.js is a very important part of the JavaScript ecosystem, as it is used in the backend to produce a complete application. It is often considered a part of the popular 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 is an open-source engine on which even Chrome works. It parses and runs your JavaScript inside a Node environment. 

    Overview on Node.js

    Node.js was created by Ryan Dahl in 2009 and since then it has become very popular backend technology. Till then the backend was dominated by languages like PHP, ASP.NET and Java. It has become popular because it enables a Frontend developer with JavaScript skills to easily create full stack apps.

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

    Node.js came into existence when its creator Ryan Dahl, understanding the power of V8, powered the Chrome browser and extended it so that it can run on your machine as a standalone application. 

    Another part of the definition on the official Node.js website says,Node.js uses an event driven, non-blocking I/O model that makes it lightweight and efficient.

    I/O refers to input/output and this is where the additional functionality of Node.js comes into play. We can read and edit local files in Node.js and also do an HTTP request to an API. 

    The earlier backend systems like PHP and ASP used to block the program till a network request was complete. But it was completely changed by Node.JS, which sends the request and then goes to the next line of code. So, it is non-blocking and faster than the earlier backend technologies. 

    But it is a single-threaded technology and that is where it has some limitations, whereas Java shines because of it being multi-threaded. 

    Yet another part of the official definition on the Node.js website says,Node.js package ecosystem, npm is the largest ecosystem of open-source libraries in the world. 

    Over the past decade, an amazing community of open-source enthusiasts have created more than 1 million npm packages, which enhance the capabilities of Node.js.

    It is completely open-source and anyone can use it, as it has an MIT licence, for developing server-side and networking applications. 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 based browsers (like Brave), but was used to build Node.js for server-side coding. In fact, the V8 engine, is also used by JSON based No-SQL databases like Couchbase and the popular MongoDB. Besides this, V8 also powers the popular desktop application framework Electron and the latest server-side runtime environment of Deno. 

    V8 is JavaScript engine, because it takes our JavaScript and executes it while browsing in Chrome. It actually provides a runtime environment in which JavaScript executes. The great thing about this is that the JavaScript engine is independent of the browser in which it executes. This is the feature that prompted the creator of Node.JS to choose V8 engine to power Node.JS and the rest is history. The popularity of Node.JS exploded and the V8 engine was also used to create desktop frameworks and databases.

    There are other JavaScript engines like SpiderMonkey used by Firefox, and JavaScript Core used by Safari. Microsoft’s Edge was originally based on Chakra JavaScript engine, but has been recently 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 ways – the first way is to implement as a standard interpreter which is done by SpiderMonkey from Mozilla. The other way is the Just-in-time (JIT) compilation, which converts the native JavaScript code to machine code and that is the way V8 uses it. So, the difference between V8 code and others is that it does not produce any intermediate code. 

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

    The V8 engine also uses some other components, along with the ones we had seen above. They are Liftoff and Orinoco 

    • Liftoff is responsible for machine code generation in a highly optimized way. It generates code for each opcode and perform way better then Turbofan.
    • Orinoco is responsible for garbage collection. It looks for disconnected memory allocations and perform operations to free up more space. It also update the pointers to new memory locations.

    V8 also uses a lot of different threads and they are – 

    • The primary thread fetches and compiles the JavaScript code.
    • There is another thread which is used to optimize the running code, while the primary thread continues its execution. 
    • Yet another thread is used for profiling, which tells on runtime the methods that are needed to be optimized. 
    • Some of the threads also do garbage collection.

    The Just-in-Time Paradigm

    We will learn a bit more about the Just-in-Time (JIT) compilation in V8. For a code to execute in any programming language, it must be converted into machine code, which the computer understands. There is a different paradigm for this transformation. 

    Most of the traditional languages created before JavaScript like C++ and Java, perform something called Ahead-of-Time compilation. Here, the code is transformed into machine code before the execution of our program during compile time. Anyone who has worked with Java or C++ knows that we run commands like below 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 can run our program with commands like below.  

    java MyJavaProgram 
    ./mycppprogram 

    On the other hand, in languages like JavaScript and Python, each line of code is executed at runtime. This is done because it is impossible to know the exact code before execution. In a browser, you never compile a code first and then run it, because it is done automatically behind the scenes.

    So, the Ahead-of-Time compilation produces more optimized and fast code, because of the compilation done before hand. Which is why interpretation done by languages like JavaScript are slower.

    To overcome this problem in dynamic languages, the approach of Just-in-Time (JIT) compilation, was created, which combines the best of both interpretation and compilation. So, an interpretation step runs before the compilation step, where the V8 engine detects the more frequently used functions and code and compiles them using information from previous executions.

    During compile time, this code is re-compiled for optimal performance.

    What is the relationship between Node and V8?

    The Node.js is referred to as a runtime environment, which includes everything you need to run a program written in JavaScript.

    The core powering Node.js is this V8 engine. The diagram shows a comparison with the Java Virtual Machine (JVM), which power the Java Runtime environment. Beside the V8 engine the Node.js runtime environment adds many Node APIs to power the Node.js environment. We can also extend the functionality of our node code by installing additional npm packages.

    relationship between Node and V8

    One thing to understand is that V8 is essentially an independent C++ library, that is used by Node or Chromium to run JavaScript code. V8 exposes an API that other code can use, so if you have your own C++ program, you can embed V8 in it and run a JavaScript program. That is how it is done by Node and Chrome.

    Suppose, we want to add a functionality in our JavaScript code to have statements like print(‘hello world’), in addition to the console.log(‘Hello World’). We can add our own implementation of print function in C++, in V8, which is anyway open sourced. 

    Can Node.js work without V8?

    The current Node.js engine cannot work without V8. It would have no JavaScript engine and hence no ability to run any JavaScript code. The fact is that the native code bindings, which come with Node.js like the fs module and the Net module, rely on the V8 interface between C++ and JavaScript.  

    Although in the tech world everything is possible and Microsoft in July 2016, made an effort to use Chakra JavaScript engine (which was used in Edge browser at that time) in Node.js and replace the V8 engine that project never took off and Microsoft Edge itself recently moved to Chromium, which uses V8 JavaScript engine.

    The new kid on the block for server-side programming is DENO. Many consider that it could be a replacement to Node.js in the next 2-3 years, and it also uses V8 JavaScript engine under its hood.

    Looking to land a job in the tech industry? Look no further! Discover the best Python course that guarantees job opportunities. Start your journey today and unlock endless career possibilities. Don't miss out!

    Summary 

    We have got an overview of Node.js runtime environment and V8 JavaScript engine in this post. Then, we have gone through the working of the V8 engine. We also investigated details of the Just-in-Time compilation, used by V8 JavaScript engine. Also, we have understood the relationship between Node.js and V8 engine and how V8 engine is independent of Node.js.

    Lastly, we have learnt that it is not possible for Node.js to run without a JavaScript engine like V8. It can, however, be replaced by another JavaScript engine like Chakra from Microsoft; even though this is highly improbable,  it is still possible. 

    Profile

    Nabendu Biswas

    Author

    Nabendu Biswas is a Full Stack JavaScript Developer, who has been working in the IT industry for the past 16 years and has worked for world’s top development firms, and Investment banks. He is a passionate tech blogger. He is also a tech youtuber and loves to teach people JavaScript. He is also an Apress author with three Gatsby books published. 

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon