Dive deeper into NodeJS - V8 Engine

Karim Muhammad
Nov 6, 2023
4 min read
post_comment3 Comments
post_like3 Likes

In order to have a proper mental model ofNodejs, we first have to have an accurate understanding of it's core, Heart ofNodejs, It'sV8 Engine

#Micro-Processor

A Tiny Machine.

as we know, each machine either (computer, laptop, mobile, etc...) has something calledprocessorand it is responsible to doing the calculation all the time.

It is exist in your machine, and you can hold it, it is physical.

We giveMicro-Processorinstructions.

Micro-Processorspeaks a language, Not all micro-processors are the same.

They don't speaks the same language, there are different ones like (x86-64, IA-32, ARM, MIPS, ...)


#Machine Code

Programming Languages spoken by computer processors

Every program you run on your computer has been compiled into amachine language, must to be compiled into it; because themachinehas all responsibility to run your programs

and sometimes it converted to another different programming languages (Intermediate Language), so that your program can run on different computers, many different processors for example.

Writing with machine code like this

low-level-code

so hard; because it is so close to micro-processor.

this language which you seen it is for specific micro-processor language and there others for micro-processors as we said.

Machine code it is not abstracted at all, you know everything, you control on everything in your machine

the more abstraction layers, the less controlling. means thehigh-level languagecare about many things that you may not know it at all, andlow-level languageyou are almost controller for everything.

Assemblycame in after Machine Code, so close as well, but more easier tomachine code.

after Assembly, C/C++ (easier) after, JavaScript, ... (easier and easier)

Why JavaScript is Easier? JavaScript is so far from Micro-processor, so it cannot manage memory spaces, or threads, and so many, so that must have anEnginebetween your code and micro-processor, to take care all of that for you.


#C++

NodeJSis written byC++means, Nodejs is a program is written inC++

V8: the C++ library, Nodejs uses it to provide the JavaScript implementation.V8provides the mechanisms for creating objects, calling functions, etc. V8's API is documented mostly in thev8.hheader file (deps/v8/include/v8.hin the Nodejs source tree), which is also availableonline.

libuv: The C library that implements the Nodejs event loop, its worker threads and all of the asynchronous behaviors of the platform.

WhyNodeJSwritten inC++? becauseV8itself is written inC++too!

#V8 Engine

V8 engine converts JavaScript code to Machine Code!

To achieve faster JavaScript execution speeds,V8translatesJScode to more efficientmachine codeinstead of using an interpreter. Most modern JavaScript engines likeSpiderMonkey or Rhino follow the same approach, but what makesV8stand out is that it does not produce anyintermediate code.

It convert your code into Native Machine Code!

because of thatV8is Fast! V8 see what your micro-processor of your system and compile code depend on that.

Engine Handle so many things:

  1. Memory heaps
  2. Garbage Collector
  3. Compile JS and Execute
  4. Call Stack
  5. Event Loop (sometimes implemented by Browser)

#Just in time compilation

TheV8engine gets its speed from the Just in Time (JIT) compilation of JS code to native machine code. The ignition interpreter, a key component ofV8, compiles the JS code and generatesnon-optimizedmachine code. On runtime, the machine code is analyzed andre-compiled for optimalperformance. This optimization is handled by theTurboFanandCrankshaftcomponents ofV8.

Your code may have many issues may cause bad performance, but to know that, you should know how V8 compile your code correctly.read

V8engine is distinct from other JS engines as itdirectlyconverts scripts tomachine codewithout producingintermediate code.

Info>

at the end, There are many years.. many many years to implement something like that, so many efforts to produce something amazing like that

the team of project V8 was consist of 12 engineers!

#Resources

  1. Docs
  2. GeeksForGeeks

#nodejs #part1

You are not logged in.