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 of Nodejs, we first have to have an accurate understanding of it's core, Heart of Nodejs, It's V8 Engine

#Micro-Processor

A Tiny Machine.

as we know, each machine either (computer, laptop, mobile, etc...) has something called processor and 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 give Micro-Processor instructions.

Micro-Processor speaks 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 a machine language, must to be compiled into it; because the machine has 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 the high-level language care about many things that you may not know it at all, and low-level language you are almost controller for everything.

Assembly came in after Machine Code, so close as well, but more easier to machine 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 an Engine between your code and micro-processor, to take care all of that for you.


#C++

NodeJS is written by C++ means, Nodejs is a program is written in C++

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

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

Why NodeJS written in C++? because V8 itself is written in C++ too!

#V8 Engine

V8 engine converts JavaScript code to Machine Code!

To achieve faster JavaScript execution speeds, V8 translates JS code to more efficient machine code instead of using an interpreter. Most modern JavaScript engines like SpiderMonkey or Rhino follow the same approach, but what makes V8 stand out is that it does not produce any intermediate code.

It convert your code into Native Machine Code!

because of that V8 is 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

The V8 engine gets its speed from the Just in Time (JIT) compilation of JS code to native machine code. The ignition interpreter, a key component of V8, compiles the JS code and generates non-optimized machine code. On runtime, the machine code is analyzed and re-compiled for optimal performance. This optimization is handled by the TurboFan and Crankshaft components of V8.

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

V8 engine is distinct from other JS engines as it directly converts scripts to machine code without producing intermediate 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.