Saturday, May 18, 2019

Node & its roots. 😃

(WIP) So, Node Js is one of the best thing that has ever happened to Technological world. There is no doubt in that.

Now coming to the language, NodeJs is the JAVASCRIPT language with the ability to run in server side ( unlike in FRONT end / browser).

So, how it does what it does, I mean, how Node JS actually gave us the ability to run the stuff in an Event loop and gives us the capability to code for callbacks, when languages like Java, Ruby are so popular in Synchronous world.

Let us first take a step back and open the Box named NodeJs and see what it has in all:
  1. v8 (JS Engine)
    • It is a JS Runtime that is needed in order to do Parsing, Compiling and Running JS Code, it is written in C/C++. In some of the cases they are also termed as VM's of JS engines.
  2. libuv
    • It is an Async IO library that is based on Event Loops, it is written in C. 
And that's it. Yeah, believe or not these are the 2 pillars of Node Js and its magic behind the scene. Well once you read this blog, you will understand why NodeJs is single threaded and why it is always said don't do heavy operations in Node JS.

Now let's see what is V8? 
  • It is a JS Runtime that is capable of running the code written in JS format but its own code is written in C++.
  • It is C++ library, that can be added as a normal library in any C++ project and you can use that to compile and run JavaScript code.
  • It is the same engine that is used in Chrome Browser behind the seen to execute the JS. 
  • It is JIT ( Just In Time Compiler) that means Compile which means 
    1. Compile Little Bit 
    2. Run 
    3. Collect data which can be used for next run
    4. Repeat 1 
  • Implemented for JS that follows EcmaScript Specification
  • In case EcmaScript updated V8 is updated
  • It is a Garbage Collected Language that means unused objects are Garbage Collected and this operation is pretty heavy in execution and automatic in nature.
  • It is not having DOM, Console or File System Access where DOM and Console is the responsibility of the Browser, File System Access is given by Libuv.
How the code is moved from Parsing to Execution?

In the first phase,
  1. the JS code is basically taken in and parsed to create AST (Abstract Syntax Tree) 
  2. AST then interpreted and converted to byte code to get better speed since it is an optimised version of our code for better understanding by the machine 
  3. and then finally code is executed. In case if a function or part of code is being used frequently, then that function is passed to Turbo Fan component, which compiles that function with the information that is calculated in between the compilations of JIT architecture, to run more faster, so basically it acts as a cache for common used functions.
In the context of Javascript, we know that browser mainly executes JS without the support of low level API's like File system. 


So the question that comes to mind is that, do we really need Browser to execute Javascript?
Well the answer is straight NO

So if Browser is not needed to run the JS code then who is responsible to run the JS code, so the answer is the VM (Virtual Machines) of the JS engines (V8, in case of default distribution of Node, Chakra is another that can replace V8) 


Node Js does most of the task by using LIBUV and by using V8, it gives a non blocking interface (async) that can be called from JS Code. 

Now the question that comes in mind is that 

No comments:

Post a Comment