How Node.js Works: V8 Engine, Event Loop, and Worker Threads Explained!

Thapa Technical
63 followers
Dec 15, 2024
Hello what's up guys today in this video
we will see and nut juice application box
yes guy today's video
is very important but before that don't check out the other
previous videos and even yesterday's video
behind the scene please
also. If you definitely check it out, you
can see that everything is visible to you in a graph
here, how
your note works as it is, so
you can see it very clearly.
First of all, here we have an application.
Okay, basically this application, you understand
that with Let's you are coding in JavaScript,
okay and with Let's you are making a simple
to do app. Which page guys to
do? The app is simple, so when you create this to-do app,
what kind of code do you write? You
obviously write the code in JavaScript.
Right now, tell me one thing in JavaScript
that you use in console dialog or anything. Document:
Gut element by ID, your computer may
not understand it, right, your
computer will not understand it obviously, so now what
happens is that the JavaScript
code goes into your v8
engine and basically what is v8? An engine
is for example, your car or even your
bike, the more powerful the CC, the more
powerful the engine is and the better it
is. Right, then you should understand that V8
is such an engine which Obviously
Google's crypt converts it
into your machine code, so
now your system will be
able to understand what you have written. Alright
guys, now guys, after that comes what is your
nut code binding? First what is
this binding, what is the meaning of this binding?
See guys, the main function of binding
is to connect your JavaScript in to your
underlined system. Now that system is something. It may also be
possible that whatever your operating system
is, it
does it fine, but now
you have to request the operating system
that I want to read the data of some files
or I want to make some network calls.
Okay or I have some process
which has to be performed or run, now
that guy cannot do your B.Ed, it is
obviously related to your operating system
right or say it is related to your server,
then there is this Binding is
it will make sure that your JavaScript gets
connected to your system
so that both of them can communicate with each other
and here by
JavaScript I mean your nut
JS. Have a node
just now. Now this binding makes
sure that the node can now
communicate with your operating system, whether it is
requesting a file
or If you have to make a network request or
perform some process, then you
can perform those operations very easily with the help of this OS. Okay, now
after that, this is very important here
because the task keeps coming one by one and
so on. With the help of this leave ub
library which I told you about yesterday also
which was created with the help of your c
so it's your c library which
they named leave uv and because Off
this library only note: Now guys, now your
can perform synchronous operations
very easily. Now guys, understand
that you have made a request to your operating system or server
that I want to read a file,
what to do is read. It is okay to do it
and I have come till now, now how to read it
asynchronously? And let's imagine,
you have asked me to read a lot of files,
so now everything will be
read at once, otherwise, what will happen now? The
part which is the note, what the binding will do, the
whole task which is one by one
in the event queue, one by one, 3 for four, in this way, one
by one, it will go into the queue. Obviously
you must have seen the queue, even you have
also heard in the data structure when If we are somewhere outside
in a school, we are
engaged in the queue one after the other in height wise, no
first come first or kind of, then in the same way,
all the tasks which are asynchronous heavy weight
tasks are one in your event queue. Buy one
now guys that is stored there
now now here whatever is most important which
is your heart aapke note jas ka jo
heart hai and that is your event loop so
what is the function of event loop the function of event loop
is that Of all the tasks in your event, is
there any one left that
I have to process? Then it
keeps checking the event loop continuously. And the
two most important tasks of the event loop are
that Check whether the task is a
blocking task and a non-blocking task.
If it is a non-blocking task,
then do a simple call back from lets, then it gets
executed immediately here itself, will pass it here, will
go from here to here and then return to
you BA. And then you will get it in your application, so in
this way that communication will keep
happening but let's guys that now
your a blocking operation is a blocking
task
right like here with file systems
something Be it any work or some network related work or
some process related work, the event loop does
not execute it immediately but you are able to see it.
This is your worker thread
here, what does it do, if it passes it, then
now here is the worker thread. Will do its work
and one by one it performs all the tasks
and and once that task is
completed then guy we have this
execute call back here you
can see very clearly basically now
What happens here is that once the work is completed then the
worker thread will place the
callback function in the event queue. Okay
and now what that event queue does is that
this callback function can easily
handle it. If he takes it, he
executes it and returns it back. Here, from
here, you go directly to pay here and
then simple guys go here to pay you
with the lets that was in your application that brother, let
me read the file. That data has
reached here, you will see it in the console
or wherever you wanted to show it, so
guys, this is how your
application works, I hope you have
understood, this is a quick recap, I am
quick. Let me give you a recap, it is simple,
no big deal, okay look, first of all you
write your code in JavaScript,
from Let's you have to read a file,
okay which can be in your server
or somewhere in your operating system. maybe
then it will happen First of all, the code you have
written must be written in JavaScript.
Now understand that if we talk about the note itself,
I am telling it with the help of JavaScript. It is
okay, if that code does
not understand your system then v8
JavaScript engine which
converts your JavaScript code
into machine code so that your system can
understand it. Now the system has understood that it has to
read this particular file, so
now what will happen is that this particular
nut This is the binding of Jas Basically, the
binding that you see here will
make sure that your JavaScript,
which is basically your operating system, gets
connected to it and once it gets connected,
then obviously it will now be
able to access the files and data here. Content is everything
isn't it and after that all the guys will be your
task and where will they go, first of all they will
go to your event queue one by one, okay
here they will go one by one and after that the guys will
now go to your event queue. What is most important is the event loop,
that is the heart of the node,
where it makes sure that there is
some task left in the event queue that
I need to get to its right place, if it
is as simple as a call back, a simple
call. If it is back type, it will execute itself
and it will return. You understand, it
will execute itself, it will return this here,
it will return this from here,
and it will return this from here.
And whatever you wanted, you will get it on the system
but if there are some blocking
operations like dealing with file system
or something involving network calls or processes then
what happens are they are known as
blocking operations and note in which
sync should run continuously.
Operations Right So what does the event loop do?
Pass it on to the worker
threats. Now what does the worker threats do? It will
do its work. Simple, whatever task is given to it,
one by one. Will complete then
again in the form of call back will return the
event loop here, so it will obviously
execute it very easily, then
return from the same, return it from here,
return it from here, return it from here, and
in your application, you have It was written that I have to
read this file, show me its data,
then you can see your data in this manner,
so ya guys, this is it,
that's all I hope for this video today, I
liked the video very much. I will
share it in PPT and the rest of the
previous video and even yesterday's video
was very important,
definitely check it out, so guys, please like the video if you
like it, share it with your friends
and our next target is
8 lakhs. Subscriber, if you have not subscribed yet then
please do so,
I will meet you in the next video till then guys,
thank you so much take care.
[music]
