Node.js Under the Hood | Understanding Node.js Core Concepts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the previous video we installed node.js and created our first simple application with it now in this video before we move on any further i want to take some time and reflect upon what exactly node.js is what this thing really is that we're trying to get ourselves into now we could just cut the conversation right now and say that node.js is an asynchronous event-driven javascript runtime or nodejs is a javascript runtime built on chrome's v8 javascript engine but unfortunately that probably doesn't make any sense we have a lot of words that we're not familiar with we have javascript engine what is this javascript engine what is event driven what is asynchronous and what is v8 and even what is really javascript itself so now we're going to have a long discussion answering these questions we need to see how a node was created what sort of problems it solves that javascript otherwise couldn't solve and what do we mean by these strange words that we have on the screen and just a quick disclaimer in this video we're gonna dive into the node source codes and have some fun i will also see some c plus plus code but you don't need to know superfluous at all as long as you can understand the comments that i'm going to write you are absolutely fine just watch the video and you don't even have to follow along and do the things i do and our discussion would probably come along and if at any point you feel lost confused and you're telling yourself hey why exactly are we doing these i just want to learn note well please just bear with me till you enter the video and trust me on this one you're going to see and realize the importance of this discussion as we move along with the course it's really important to first see what node.js really is okay let's start from the minimum from the very basics processors can only understand machine code now i want to use the terms microprocessors cpus or processors interchangeably but they have some differences and for our purposes it doesn't matter and we refer to them as the same thing so these processors could only understand machine code and each processor has their own unique language and we have different processor architecture and each one of these architectures could only understand their own language so we have architectures like arm 46 mems x86 and a couple more so each one of these have their own language so a manufacturer of a processor might say that hey this is how we could talk to my processor for example if you want to tell it to add two numbers together here's how you could do it with the available instructions that it has and actually machine code is just zeros and ones now for the sake of simplicity we usually use some codes and hexadecimal numbers instead of just writing straight zeros and ones but ultimately they're just zeros and ones a cpu already knows that for example if i get the character x r it might mean this specific zeros and once and in the real world you really need to write machine code well except if you want to create a compiler or you really just want to talk directly to the cpu in most cases using another language a little bit higher level is going to get your job done now on top of machine code we have assembly language which is incredibly low level and to convert an assembly code to machine code we don't actually use a compiler we use something called an assembler and what it does is that it takes the code that you write and converts it straight to machine code or just searching once so we have some instructions and assembler takes them and converts it exactly to the machine code for the specific processor that you're coding for then if we go a level up we have languages like c and c plus plus with these languages you actually have access to the operating system and the hardware so you have maximum amount of control over your memory over for example if you want to go ahead and fetch something on the hard drive or go ahead and talk to the network card to for example send a request to the internet you could use these languages so you have a lot of control with cnc plus but the more control you have the more responsibilities you have so you have to be careful if you want to write code in cnc plus plus because you have so much control you might mess some things up if you use cnc plus you need to use something called a compiler now compiler takes your code and converts it into an assembly language and then it converts that assembly language using an assembler to machine code now if we go and level up we have very high level programming languages like javascript and python now these are incredibly high level and usually these languages you don't have access to the hardware for example with javascript you can't just access files you can't send a network request because to do those things you need to be able to talk to the hardware and with these languages you can't do that now with cnc plus you could really create anything that you want be it an operating system if you want to go ahead and create i don't know for example linux or android from scratch you could you're going to be just fine with just seeing as assembly language you're not going to need to write machine code so much well maybe in very rare cases but using just assembly and c you could literally do anything that you want now for some very complex applications like video editing applications or photo editing applications they need to have maximum amount of control over hardware and they need to have maximum amount of performance those are usually written in cnc plus plus but in a lot of cases you don't actually need to have access to that much control and power and you're gonna be just fine creating your application in javascript python or many other high-level programming languages so we have kind of like this hierarchy in order to get to javascript so now what i want to do is to show you two examples one in assembly language and one in c plus plus but don't be scared it's gonna be okay i'm just gonna show you some code and you don't have to do anything just watch me do it and i'm trying to make some points here right so let's first see the application written in assembly language so here it is i have created a folder on my desktop called assembly demo and i have created a file called helloworld.s which is an assembly file and all it does is that it outputs the hello world message to the console so that's all it does just a lower application and assembly if i open it notice that it looks really crazy don't worry about the syntax you don't have to understand anything here well except a couple of things first of all this whole code 20 lines of code all it does is it goes ahead and prints hello world out now if you read the comments you would kind of figure out what we're doing here here it says simpler program to print hello world then we're setting up some parameters to print hello world and telling the kernel the operating system to actually do it then we have some parameters here we have this hello world think of it kind of like a variable then the length of our string you even have to specify that one then here we're telling the operating system to actually do it and right here we're setting up some parameters to exit the program so you can't just write the code and say that's it you have to specify how you want to exit then here's our final string which is hello world now notice here we have some hexagon numbers and character encodings which are really important to understand but we will talk about them in the buffer section and not right now so yeah that's it notice that we're doing a lot of things just to get a hello world out and it's even a lot more work if we try to run it now one last thing i want to point out here is that when we want to run this application when we want to run it through an assembler to get a binary executable file all it does is that it converts these commands to just zeros and ones so might for example take this move and say instead of move put this one ultimately this file would get converted into just zeros and ones now let's go ahead and run it and see how much work that will take so i'm gonna open up my terminal you don't have to do this just follow along and i'm gonna paste in some commands i'm not going to write them because it could be kind of long i want to first run it through an assembler using the as command i want to say that the output should be hello world.o which is an object file which you don't need to understand just think of it like an object file that we would give that again to the sampler and then it's going to give us an executable file and this is our input file hello world.s and if i run it now i'll get this hello world.o which is an object file and the last command that i need to run which is a lot longer is this ld command and the only two things that i want to say here is that we have to specify our operating system and also our cpu architecture so you can't just say that i'm writing assembly code just like the way you say i'm writing javascript code you have to say i'm writing assembly code for this operating system and also for this cpu architecture because each operating system and each cpu architecture needs its own unique commands for example if i want to run this program but for an intel based cpu some of these might look different but now let's go ahead and run it and now i will get an executable file so if i run this hello world i'll get hello world so this is our assembly application let's also take a look at the file explorer just to see what we have so we have our dot s file which is our assembly source code and then we have the object code and then we have a unix executable file i could just double click on this [Music] and it's going to run it and here's our hello world message all right so that was our very simple assembly application and now let's move up a little bit and talk about cnc plus plus notice here that we're trying to ultimately get to javascript and node.js but we need to lay a foundation of knowledge before we get there so what i want to do right now is to show you a simple c plus plus application just like i did with the assembly language and we're just trying to understand some simple things okay so here we go so i have created folder called file creator and inside of it i have a simple c file called file creator.cpp if i open it notice that it looks a lot more like javascript than assembly does and what it does is that it asks me for a file name and some content and then it goes ahead and create a file on my hard drive with that contents and that file name so here just read the comments this variable here determines whether we should continue creating files or exit and then we have a while loop just like javascript here's the variable that saves the name of the file the content don't worry about the syntax here and then here it asks for the file name and then the content and then here it creates the file that's all so this is a very simple c plus application and now i'm going to go ahead and run it so i'm going to open up my terminal and the way we could run c plus is using compiler the compiler i use is called g plus plus so i'm going to use g plus plus and output a file called file creator and i want to give it the contents of this cpp file now i'm going to run it and now just like before we have an executable file i could double click on it run it and be able to create files and now i'm going to go ahead and run the application and all i need to do is to specify the executable file name in my terminal so file creator and i'll run it all right so here it is it has a very simple menu i could either create a file or exit i know it's a pretty silly application but let's just stick with it and try to understand some things in cnc plus plus because javascript and node.js are written in c plus and later on when we get into the node.js source code we're gonna have a much easier time understanding how things kind of work together if we have some understanding about cnc plus plus okay so let's just go ahead and create a file now it asks me for the file name i have to specify the path along with it so users joseph desktop and then i'm going to call it file1.txt and then the content that i want to put in the file i'm just going to put this is the first file [Music] so the file was created successfully i'm going to create one more file i'm going to specify the same path but i'm going to call it file2.txc and this is the second file and now i'm going to exit okay so now if i go to my desktop i could see that i have now two text files on my desktop file one which contains the string this is the first file and also the file too this is the second file so pretty simple pretty easy all right but notice now that i have this executable file i could just go ahead double click on it and i could run it so if i open it i could double click just like before with assembly language we get a unix executable file i could run it and just specify the options and play around with it now the interesting thing here is that i could now cut this file out maybe put it on my hard drive or on my usb then completely wipe out my computer my laptop and then be able to paste that file in back to my computer and be able to run it without needing to compile anything so this is an application i could move it around and run it and everything is going to work just fine but if i want to move this file to another laptop i could even move it to another laptop that uses the same operating system and the same cpu architecture as i do and that laptop is going to be able to run this file just fine but i can't just take it to any laptop if a laptop doesn't use the same operating system perhaps it might be a windows machine or it doesn't have the same cpu architecture as i use for example it might have an intel based cpu that laptop is not going to be able to run this file so we have to get a different build for various different cpu architectures and different operating systems now remember that we said when you compile a c plus plus application the compiler would take your c plus code and then it would convert that into assembly and then an assembler would convert that assembly code into machine code now g plus plus is doing all that behind the scenes and if you want to see the assembly output of our c plus plus file we could do so by running our compiler and specifying the dash s command and then my outputs which is going to be file creator dot s for assembly code and file creator for the executable file and then the input now if i go into visual studio code you could see that we have this dot s file if i open it notice that it looks kind of like before that assembly file it's actually assembly file so if i search for move we have a lot of that commands here and there so ultimately your c plus application would get converted into an assembly file and then this assembly would get converted into machine code and if you're curious to know this 15 line program got converted into more than how much 8 000 lines of code i know it's kind of crazy but yeah this is why we don't write assembly code and we use languages like cnc plus plus because writing all of these yourself just to be able to create some files is pretty crazy but actually one of the reasons that it's so long is because we're including some files some c plus files so if i just go ahead and create something extra simple like something that defines variable a and variable b and then add them together and save them in variable c like this [Music] and if i get an assembly file off of this file it's going to be a little bit simpler still way more complicated than just these three lines of code but you know it's assembly language and you're talking directly to the cpu all right now another fun thing we could do is to add this file add this executable file to our path so that we could always have access to our application by just running file creator in fact let me just go ahead and do that if i want to do it i need to add it to my path so i'm going to go into my hard drive and then user local bin now i'm going to copy this file or cut it and move it to this bin folder so i'm just going to move it like this i want to put in my password [Music] and now that i have it in my bin folder i can just run file creator and be able to create files let's test it so here's my desktop i have two files i'm going to call it let's put it on the desktop again file 3.txt and it's now created another one [Music] file this dot txt and exit pretty cool right so at any point in time right now i could just run that command and build great files so we did a lot of talking but now we're ready to talk about javascript and then node.js so let's see what's going to happen when you write and run javascript code so as you know javascript is a programming language meaning when you write javascript you're giving instructions to your computer to do something be it adding some numbers together or modifying a string sorting an array etc and you also know that computers could only understand zeros and ones so in order to be able to run javascript we need a way to be able to convert our code into machine code something that computers could understand so how do we do that how do we get our code into that machine code in assembling language we saw that we use an assembler to convert our code into zeros and ones in c plus plus we used g plus plus which is a compiler that converts our c plus plus code into machine code now in javascript we use a javascript engine to do that javascript was first created by bretton ike in 1995 and he created this engine called spidermonkey which was the first javascript engine ever brandon was working at netscape at the time which was owned by mozilla and then later mozilla took that engine and used it on its web browser firefox so firefox now uses this javascript engine spidermonkey and ever since javascript was invented in that year a lot more javascript engines were created actually you could create your own engine too something that could take javascript codes and convert it into something the computers could understand now a javascript engine that we're going to talk about and node.js and google chrome are using it and it's also the fastest javascript engine out there is v8 v8 was introduced by google in 2008 along with the browser google chrome and a year later ryan dial creator of node.js used that v8 engine to create node now to see how he did that and how exactly v8 is being used in node let's go ahead and take a better look at v8 so by now we know that v8 is a javascript engine all right let's take a look at the website of v8 so i'll open up my browser and search for v8 javascript engine [Music] i made a typo but it's gonna find it here we go it's v8.dev so go ahead open it and here's a website so let's try to read this paragraph and try to understand more about what this v8 really is so it says that v8 is google's open source high performance javascript and webassembly engine so we know that v8 was created by google and it's open source meaning that you could take the code you could modify it and you could build upon it and we're going to take a look at its source code in just a minute and it also says that it's high performance now we said the v8 is the fastest javascript engine out there and per reason that it's like that is because it's converting your javascript code straight into machine code now yeah i know we said that everything is going to get converted into machine code but in the case of other javascript engines such as spidermonkey it doesn't convert your code into machine code what it does is that it converts your code into something called bytecode and then a virtual machine is gonna run that bytecode so there's kind of another layer between your javascript and then that zeros and ones and that is a virtual machine that's why it's not as fast as v8 all right let's move on here it says it's javascripts and webassembly engine so we already know what a javascript engine is don't worry about this webassembly thing we're going to come back to it later on in the course but basically it would allow us to write c plus inside of our javascript codes which is pretty cool actually but we're not going to talk about it right now written in c plus plus so v8 is written in c plus plus that's actually for the reason that we saw that c plus plus code at the beginning of this video and we're going to see more of that when we take a look at its source code so let's keep going it is used in chrome and node.js among others so some applications are using this v8 but two of the most famous ones are chrome and node.js among others it implements ecmascript and webassembly it's important to understand what this ecmascript is and what it really is is that so we have a lot of javascript engines out there right firefox use this one google chrome uses one safari uses one now in order to create unity and consistency of the way these engines execute javascript code we use a set of rules or this ecmascript which is a language specification it defines for example how should a function work how should the syntax of a function of an object be like so we have this ecmascript so that developers javascript developers could make sure that if they create a program in javascript it's going to run in the exact same way that it's going to run on these different javascript engines that implements this ecmascript thing and if you want to take a look at the specifications and rules that this ecmascript specifies just search for ecmascript specification and here's a website international.org here it says it's a language specification and if you scroll down you could find all that specification either in pdf file or in html i'm going to go ahead and download this so it's a standard and if we scroll down to the content section you could see that it's a pretty long file more than 800 pages for example here it says how a boolean type should be if you click on it here it says type consisting of the primitive values true or false so that should be a boolean type so javascript engine that implements ecmascript when you create a boolean type it could either accept true or false so that is what ecmascript is it's just a language specification that specifies how javascript should react how javascript should be implemented and if you're curious you could come here and read all these rules all right let's get back to the v8 website so moving on it says that it runs on these various operating systems and these different processors so it runs on windows mac os linux and only the devices that have one of these processors we talked about what these are we talked about that each processor each cpu could only understand a specific language some speak mems some arms some ia32 and some x64 so if a processor speaks a language that is not one of these it's not going to be able to run v8 and now the last sentence is where it really gets interesting it says v8 can run standalone or it can be embedded into an ec plus plus application let's see what exactly this last sentence means what do we mean when we say we could embed v8 into an ec plus application and this is actually how node.js was created let's go ahead and take a look at v8 source guide so what i'm going to do is to search for v8 source code and here i'm going to open up this link in github now if you scroll down here it says how you could get the code you could go ahead and clone this repository but then you wouldn't be able to actually run it so you wouldn't be able to get a build out of it and run that v8 application so if you want to be able to build it get the code using gclient i'm not going to show you how to do it i've already done it and i also built the project so let's go and take a look at the source code so here i have opened all the source codes in my visual studio code and yeah here it is here is all the source codes that make javascript possible in v8 and it's a lot of code if you come here and if you start to take a look at different files you kind of realize that it's a lot of code the source the main implementation is here in the source folder if i'm not mistaken it's about 100 000 lines of c plus code so it's a lot of code here in this source folder we also have a lot more codes here in this test folder for testing the v8 i'm not sure how much but probably more than a couple million so we have a lot of tests written for the implementation of v8 all right so let's take a look at the source here you can see different things for javascript for example here we have json this is how json is implemented inside of javascript so we have json.parser and here we take a look it's a lot of code a lot of c plus code notice here i have some changes in git it's because i built the project and i changed some things but it's nothing to worry about we have let's see what else we have we have wasp which is for webassembly we will get to it later on in the course then we have objects which is a lot of code and notice here we have some files ending in dot h and some files ending in dot ccc both are c plus plus files but that h files are header files so in a h file you might define how your objects might block how your classes should operate and then in the dot css file you implement those functionalities so a dot h is just a c plus plus file but it stands for header file it's a header file okay then we have here some code for numbers then we have json and we have something called d optimizer and if you open it you're going to see all those processors that we talked about we have mems we have x64 we have arm and if you're wondering now what is the optimizer well actually v8 tries to optimize your code for as much as possible but in some cases you might do something in javascript which v8 might not really like for example if you define a variable here i'm going to write some javascript actually let me just create a new file [Music] if you for example find a variable called a and assign a number to it and then later on you change it to a string v8 will do some de-optimization right here and you could do this because javascript is not a statically typed language but computers understand types meaning for example here in c plus plus if i could just find one file let's see what we have here if i search for end you're going to see a lot of ends so ends means integer so if you wanted to find a variable you also have to specify the type and once you do that you cannot change type later on for example this is an ends type i can't just change it later on to the boolean type but in javascript i can and that is not a good thing well it gets job done but it makes your code a little bit slower and that's one of the reasons we use tools like typescript with typescript you could ensure that you have static types in your application and once you define something for example as string you're not going to be able to change it later on and that will actually improve the performance of your code anyway let's move on so let's take a look and see what else we have here we have the date object the code for the new date when you create data objects this is what powers that functionality we also have d8 d8 is the console for v8 so if we build all of these source code we're going to get one executable file which is called d8 i honestly don't know why google called it d8 but well that's name and yeah this is the source code if you want if you know c plus i encourage you to come here and play around a little bit and try to understand this v8 thing now let me show you what's going to happen if i build this project and run that final executable file so i've already done it and i'm not going to show you how to do it it will actually take some time but when i built it i got this folder called out if i open it i have two folders x64 and r64 i got a build for x46 but my machine is not an x46 machine so this didn't work but this did let me open it in my finder [Music] so and open up rm64 dot release so there is a command that you can run from the root folder of this v8 and that command is actually using g plus plus that compiler that we saw previously is going to go ahead and build all of these code and give you an executable file in this out folder so i'll open up this one and here's d8 so this d8 is the final application of all of these c plus code here if i go and double click on it if i zoom in a little bit i have kind of like v8 console and i could run javascript if i run something that is not defined i'm going to get an error message and this should look familiar to you if you have used javascript before perhaps in google chrome console or if you have used node.js before you have probably seen this error and i have access to all different sorts of things i have access to the if statement i could run an if statement like this actually we need to specify something in the if statement so if one this [Music] notice that i don't have access to objects like documents i can't run documents.coordinateselector or documents.getelementbyid neither do i have access to the require function so if i run require http it's going to return an error it says require is not defined but the reason that we have this one in node.js and this one in google chrome is because these are c plus applications that have embedded v8 into their source code and as a result they could now implement new functionalities and bind them to javascript so that you could run them in javascript so let me show you what i mean let me show you how nodes and other apps that are making use of this v8 are structured how they are embedding v8 into their source code so i'm going to open up that c plus application that we created before so here's that file creator application that i showed you before now what i could do here is to create a new folder here for example call it dap short for dependencies and inside of this dependencies folder i might create another folder called v8 and inside of this v8 folder i put all that vh source code in it i'm not going to do it right now because it will take some other configurations and it's not really the purpose of this course i just want you to get the bigger picture about how node is structured so imagine we have this folder with all the v8 source code eventually what i could do is to include v8 right here so i could say include v8 or actually v8.h but let's not worry about the small details right now if i do this if i include vh into my c plus plus application what i could do is for example i could come here and define a function this is not how we define functions in c plus plus but it's just a pseudocode let's go with it to define a question called createfile and instead of this function i'm going to put in all of these logic and also well and also right here when we create our files so i'm going to put in all these logic right here and then i'm going to bind this create file using v8 so for example let's imagine we have a function called bind and i could say bind create file something like this so i'm just over simplifying things so that you could understand how this is working a little bit better so when i do this when i bind my c plus application to my v8 we might also have something like v8 dot bind this something kind of like this eventually what i could do then [Music] is to in my terminal when i run that file creator application [Music] imagine we don't have these things just a console i will be able to run that create file function like this and i might pass for example the first parameter which is going to be the path and the second one which is going to be the contents [Music] and when i run this in my c plus application notice that it's just javascript [Music] my application is going to see it and says okay go ahead and run this c plus function so i'm writing javascript but actually behind the scenes i'm calling c plus plus functions and c plus plus is a very powerful language in c plus i could have access to my operating system i could deal with files deal with my network card and do a whole lot of things so this is kind of like how node is structured they have included v8s into their source codes and they have created so many different functions in c spots and then they're binding those functions in their application so that you could for example call a function called require http and be able to listen for requests so you're writing this code inside of your javascripts but actually you're kind of making use of that c plus face and one more thing when you include v8 into your source code you're also going to be able to write javascript inside of your application so i could have files like i don't know like app.js and be able to run javascript here [Music] and make use of it inside of my other c files so i know i skip over a lot of things there are so many configurations you have to do and also this this is not really the right syntax this is kind of like a pseudocode but hopefully by now you know how node is structured we're also going to take a look at the node source code and i'll show you in a little bit more details how this is kind of working if you go to the node website so i'm going to nodejs.org and if you go to the docs and then here i click on the dependencies you're going to see the list of all the tools and libraries that node.js depends upon and right here in the libraries it says v8 and here it says that the v8 library provides node.js with a javascript engine which node.js controls via the v8c plus plus api it's kind of like those binding things that we talked about and here's the documentation of v8 if you click on it it gets you straight to the va.dev website and slash docs in the documentation of v8 and you could come here and read it and get more information about how you could embed dv8 into your own applications let's go back so v8 is one of the most important dependencies of node.js we also have a couple more but another one that's as crucial as v8 is the we and we're going to talk about it later in this video we have two important dependencies in node.js v8 and wv labv is also another application we also have llhttp cras these are not as important as v8s and libriuv and we'll talk about them throughout the course we also have openssl we have zlab for compressions and in tools we have npm to manage various different packages install new packages and then we have jib which is a build system and notice the building is incredibly important in c plus plus applications because you're getting different builds for various different operating systems and different processors and lastly we have also g test which is to test c plus fast code but again let's not worry about these things right now i just wanted to show you that here you can see all the dependencies that node just depends on and two of the most important ones are v8 and labv all right so we talked a little about v8 and we just scratched the surface there's so much more we could learn about v8 and we will talk about it more later in the course but not so much if you really want to dive deep into v8 go to their websites read their articles and pages grab the source code get a build and then maybe change a thing or two and you know just play around of course you need to know some c plus plus but if you know it could be a fun project hopefully by now you could see how great v8s and embedding it could really be you could add your own features to javascript for your own needs maybe you want to use javascript to create desktop applications games so much more maybe you want to program a robot that perhaps it's a small controllable helicopter which uses an armed cpu and runs linux as for its operating system usually in robot programming c and c plus plus are heavily used because of their low level features performance and power so what you could do is to take v8 embed it into your own code and then you're going to be able to code your robot in javascript now of course you have to define those low-level features like controlling the speed of the wings maybe i don't know with those languages like cnc plus plus but then you could bind those functions and all you're going to need to do later is maybe call a function called change wingspeed in javascript this is one of the reasons that javascript is becoming more and more popular each day people could just take one of these engines and build their own version of javascript maybe something like node.js or react native something that would allow you to create new things with javascript nodejs allowed us to be able to create javascript programs for servers which is pretty cool or react native using it you could create javascript applications but for mobile devices and i'm sure a lot more of these things are coming into the field okay now let's go and grab the node.js source code and also take a look at that one so i'm gonna open up my browser and in the node.js website i'm going to go into the downloads section and also here when you come here we could either download the lts version or the current version i want to download the lts version and right here we have three different ways to install node and at the end of the day what matters is that you get an executable file added to your path along with some files and folders to support that file for example if you want to install an npm package globally that npm package would go there so we need to get an executable file added to our path and actually it doesn't really matter if you don't add it to our path but if we don't do it each time we want to execute node we have to specify the full path so for example we put node on desktop we have to always say desktop slash for example home and then slash nodes and then we could get to the console now as i said we have three ways to download node we could either grab the installer or grab the binary or grab the source code and build it ourselves so what i'm gonna do right now is to download all these three options and then take a look at each one of them and see what we're dealing with so here i'm going to grab the installer for my machine so first we have to specify our machine if you're on linux mac os or windows i'm also going to grab the binary i have two options 64 bits or arm 64. my cpu is rm64 so i'm going to download this one as well and lastly i want to download the source code all right so they're all done now and now open up my downloads folder and take a look at these three options so this one's the installer this one is the source code and this one is the binaries so i'm going to double click on this one to get the source code first i'm going to unzip it and i'm just going to call it node node source code maybe and now i'm going to open up my terminal and go to my downloads folder and go to that node source code and then open it up in my visual studio code so here it is and actually if you go ahead and clone the repository from github you're going to get the same source code so here for example i have benchmark depth and dock if i search for node.js source code or no just github here is the github link here we could see the exact same files benchmark devs doc and it's just like that one so you can either come here and clone it or grab the source code from the website all right so right here what really matters and we're going to take a look here in this video we will come back to this source code of course later in the course but right now let's try to see how v8 is used so i'm going to open up the source folder actually first let's take a look here at the steps folder which means dependencies so if you open it right here you can see v8 you could also see zlip and uv which is love uv and other tools and dependencies that node.js depends on those things that we saw on the website in the dependencies section so here is v8 and if you open it that's the v8 source code that we saw previously so we have source we have test we have testing and all that source code that we saw in the v8 source code let's close this v8 and also the steps folder now let's go into the source folder and right here i'm going to try to find one of these files maybe i think node would be a good file to open up so here it is node.h i'm going to open it and i want to open the dot h file which stands for header file this is also the ccc file but let's first take a look at the h file and here if i scroll down i could see right here on line 63 that v8 is being included include v8.h [Music] and then it's being used in different places we'll have some bindings in place if you go into another file which is called i guess it was called node bindings if i'm not mistaken let's see if we could find that one here is node binding here's the dot h file and here the uv file is also included which is loopy v which we'll talk about in just a minute and also the node h file and in the dot cc file if you just search for bind you're going to find a lot of results well not all of it might be the things that you expect but you know it's binding meaning binding javascript kind of like to c plus plus then let's take a look and see what else we have if i scroll down here we have a lot of files for streams which we're going to take a look at we have tcp udp util we'll take a look at all of these later on we have timer functions like set timeouts set immediate then we have the implementation for uh workers which is another important thing that we have to talk about zealot and so much more we have node performance files and so yeah a lot of things are going on here so let's close the source folder we have crypto we'll also take a look at this crypto it's an interesting package okay let's close the source and notice here if you come to the lib folder you're going to see a lot of javascript files now these are also implementation for node.js meaning there are the node.js source codes for example cluster buffer search which is which desired we use to test node.js code notice that they're written in javascript and if you recall previously we said that if you include v8 into your project to your c plus plus project you're also going to be able to write javascript so this is why we have a lot of javascript code here in the node source code as well source code for http for dns for vfs module or file system for net os path all these are super important and we have to talk about all of them in the course and when you include a module like for example you say const fascicles require fs you're actually including these files so if you run this line you're actually including this fs module and right here it is somewhat somehow binding to that c plus phosphile so if you search for binding here you can see a binding internal binding fs and it means that it goes to the c plus phase and handles your file requests we'll take a look at these in more detail later in the course all right so this is the node source code and what you could do is to build this source code [Music] so if you come back to the website [Music] and if you scroll down here it says building node.js from source on supported platforms and if you open it it explains how you could build all that source code on your device so you have to use make which makes use of that g plus plus compiler that we saw previously and yeah you could run a whole bunch of commands and then ultimately you're going to be able to get that binary executable file that you could add to your path and be able to run node all right so that's about the node source code now let's go ahead and take a look at other files that we downloaded we have this darwin rm64 file which is the binary file of node.js so if i double click on it and unzip it and if i open up the folder right here if i go to the bin you can see that node executable file so i could double click i get access to the note shell so i could require various different packages like this and what happens when you install node is that you kind of add this node into your path and you could also install nodejs like this all i need to do is to grab this and put it somewhere that my path would understand and we also have this lib folder and a couple more that are supporting that executable file for example if you install a new global package it's going to come into this node modules folder this is one way you can install node and notice i said you don't always have to specify the path so i could just come here if i could open my terminal right [Music] here and get a directory what i could do is to run node.js from this path so i could for example from my root folder i could say this and i'm gonna be able to get to the shell or i could run files like that so for example i might have this and then.server.js so when you install node what you're actually doing is that you're getting this executable file along with these supporting files added to your path so if i run this installer what it does is that it takes those binaries and it adds them somewhere that my path would understand so here it says this package will install node.js version this to user local bin slash node so this is my path folder npm in user local bin slash npm and this folder bin slash npm is just this one bin slash npm notice it's an alias or shortcut so if i show the original one here it takes me to that lib folder so lab node modules npm and then bin the way we installed node was using nvm so if i run which node here i get the path that my node executable file is located so i'm gonna open it i'm gonna open the folder [Music] and here i have the same bin folder i have node i have npm and a couple more packages that i installed globally like pm2 and rush but if i go back up i see that lib folder here again so node modules and then pm2 and npm and other packages that i installed globally for my note version 14. if i go up a level again i'll see different versions that nvm installed if i go into version 16 again i have that bin and then the node executable file so this is kind of what happens when you install node.js on your system now js is just a c plus application so you could also come here and build all of these source code yourself and then get that executable file and add it somewhere that your path might understand right so that was a deeper look at what happens when you install node.js on your system now before we get into libyuv i just want to take a moment and tell you some history about javascript and node.js just a little history about node in javascript nothing too crazy nothing to memorize or anything like that just something sorry for fun and then after that we're going to talk about librev which is going to be the last topic of this video all right here we go so about 90 years ago the first computer was invented these computers were those huge ones that used light bulbs to represent zeros and ones or on and off switches they occupied a whole room and they would consume a whole lot of energy just to be able to do some simple calculations and the way we used to program these were actually just by writing straight binary codes just zeros and ones then some program languages came in to make this easier and one of the most important ones was assembly that came in in the year 1949 which made the job of programming for these computers a lot easier now about 20 years later the first high-level program language was created the c language which really had a great impact in shaping the computers and software and it was considered to be the first human readable programming language something that its syntax was more like english like those while and if statements now c plus is considered to be the new version of c which was published in the 1980s in the year around 1977 the first personal computer was introduced to the public which was apple one now a couple years prior to that there were also some other personal computers out there but they were more like hobby projects rather something that regular people could buy when wizarding and jobs introduced apple 1 and apple 2 it really started to revolutionize the personal computer industry and computers became way more widespread among people when bill gates introduced windows about 10 years later computers became even more popular then the internet was invented in the year 1983 and a couple years later the first website was launched at 1991 which at the time only was a very simple web page just some simple html javascript was introduced by brenton ike in 1995 with purpose to make web pages interactive and be able to program them in that same year internet explorer was introduced by microsoft and later that year the second version of it was published which supported javascript and then more and more javascript engines started to appear the ecmascript specification was defined two years later in 1997 to make sure that developers could make sure that their javascript could run as expected on different browsers firefox was published in the year 2002 and it was and still using the spidermonkey javascript engine which was the first engine then chrome along with its engine v8 was introduced in the september of 2008 and then a year later ryan dial took the engine and created node.js to make the job of creating network applications a lot easier faster and more scalable than other solutions that existed and it was somewhere around the year 2012 i'm not quite sure was 2012 or 2014 that the age developers started to take node.js into account so that they wouldn't push an update if it would break the node.js tests which was a huge deal for node.js it really made the growth of node.js faster and better previously for example nodejs wanted to give a new update but they wouldn't be able to release it because the new version of v8 would break their tests there were some problems with node.js and v8 they weren't really compatible but now it's way better than before all right so that was just a little history about node.js javascript and so many things that had an impact on it now it's just a very simple history now a lot happened behind some of these items i talked about for example if you read about how the first computer was created and how the personal computer industry started to took off it's it's actually a lot so this was just a really brief history and now we're ready to talk about one of the most important dependencies of now gs let me be i really couldn't end this video without talking about some of the crucial aspects of know to understand libeuv event loop and event driven programming and we're going to talk about them right now if you recall we said that node.js is meant to be a system language and its purpose is to create network applications in other words it's a server-side language now if you think about it server-side language needs to be able to do some things that javascript couldn't do so you know that node.js is a c plus plus application that has v8 embedded into it so let's think about some of those features that node just needs to add to javascript in order to be able to be a good server-side language for example it needs to be able to talk to different processes on a computer one of them might be a database server like mysql nodejs needs to have a way to be able to talk to that database server to fetch data from it or to write data on that database server another example would be to be able to deal with files it's really important for a server-side language a server-side technology to be able to have a way to deal with files it needs to be able to handle file uploads from the clients or be able to read a configuration file when it's starting or be able to for example save the lives of the server on a file another feature that it needs to implement is scaling it needs to be able to handle millions of users millions of requests at the same time now node.js is incredibly scalable and we're going to see that later in the course and of course it needs to be able to deal with network requests be it a tcp request or a udb request accept some requests from a port or send requests to different various urls so an ability to handle network requests is also another very important thing that node.js needs to implement now a lot of these things that i talked about are done using low uv so libya v is handling all of those things behind the scenes libby was written in c by the creators of node.js but now it's actually used in various different projects so let's go ahead and take a look at the node.js website and see what it says about libbyv [Music] so i'm here on the node.js documentation website in the dependencies section and i'm going to scroll down to find the oib section [Music] so here it says that it's another important dependency of node.js and it's a c library now notice that c is just like c plus c plus is considered to be the new version of c it added some new features like object oriented programming so you could just write c in your c plus plus applications and it would compile just like before so c is just like c plus and it's not really our discussion to talk about or differences but think of it like c plus all right now for the rest of the paragraph you're going to see a lot of words that you probably don't know like non-blocking io dns child processes pipes signal handling pulling and streaming thread pull and it would be an overkill to talk about all of these things in this video what we're trying to do here is to lay a foundation of knowledge about how nodejs is working under the hood so but so for this so for this video we're not going to talk about what these are but they're actually describing what the slow bb is doing for example pipes pipes are a pipe is actually like the name suggests it's a way of communication between two processes so i might have node.js and a database server opened up on my machine there need to be a way of communication between these two processes and those are done using pipes and it's done at the operating system level and if you're wondering to know what processes are if you go ahead and open up your activity monitor on mac or if you're on windows open up your task manager or i'm going to just open up terminal and run the top command when you do that you're going to see the list of all the processes that are opened up on your computer now in my case i have 335 processes opened up on my computer so if i count all these items i'm going to get that number all right so these are what processes are and if you run node.js you're going to get a new one right here or if you start your database server or your redis server you're going to get a new process right here on this list for example if i just run the node command i want to get a new process i could just search for node and here it is i see the node process process name is node and there are also some other important things about this process that we need to identify and we'll talk about it more later like the cpu usage or the number of threats that it has opened don't worry if you don't know what a threat is it is it's it's going to be it's a whole new discussion and we're not going to talk about it right now another important thing is pid or process id it's a unique id for each process opened up for example if i want to kill this process i could just open up my terminal and run kill and then the process id and it's going to be able to kill this process or the amount of memory that it's using if i switch to the memory tab i could see how much memory this process is using in this case it's only nine meg but if i go ahead and create new for example buffers and allocate new memory this number is going to go higher and higher another one is in the network tab i could see the number of bytes that it sends out and the number of bytes that it receives and also the number of packets that it received and sent again we're going to talk about these later right now all we're trying to do is to really understand what happens when we run node and when we execute a command or listen for an incoming request what is really happening behind the scenes when we do that so let's now take a look at the diagram and see what really happens when you start your note process and you run a piece of code so your computer that runs node.js either it's your own personal computer or it's a server and cloud doesn't matter it has a cpu a ram storage and a network card so that computer has this hardware there are also some other hardware involved in computer like your graphic cards or we could also talk about things like keyboards or a monitor but really node.js doesn't deal with those things so we have these four pieces of hardware and if you're wondering to know what is a network card it's the thing that allows you to connect to the internet or buy maybe an ethernet cable or wi-fi or a 4g or 5g connection really it could be anything but this network card would allow you to connect to the internet or just connect to different computers so you could have two computers side by side and you could connect them together using an ethernet cable using a network now on top of this hardware we have the operating system now think of an operating system like a way of communication between a program a process and your hardware it's something that controls everything on a computer it allocates resources to different processes and allows them to be able to communicate with each other or it will allocate some amount of ram some amount of memory to a specific process or it will go ahead and fetch some data from the storage maybe it's an ssd drive or a hard disk doesn't really matter the operating system goes ahead fetches that file and then it gives that to the process so we have a way of communication between programs and hardware through operating system now on top of our operating system we have our process node.js now we might also have so many other processes running in our operating system like a mysql process or redis we saw that my computer i had opened up more than 300 processes and and yours it's kind of like that number naturally a lot of these processes are opened up by the operating system itself but anyway let's now put our focus on node.js and not focus on the other processes so what exactly is happening inside of node we saw that we have the v8 engine which allows us to interact with the node application through javascript which is pretty cool and we also have the lib uv package which node just uses to handle things like file system like creating files deleting files writing files and also it handles the network things like sending requests listening for requests socket applications and a couple more things also pipes and talking to other processes and these two are the most important things inside of node.js two of the most important libraries that node.js depends on now we also have some other things like l http or zlip open ssl and see ours but really we're not going to focus on them right off the bat right now we will talk about them later in the course for example we're going to talk about zelib when we're talking about compression or we're going to talk about openness asl when we want to talk about crypto hashing strings and all that kind of good stuff but right now these are not really as important as v8 and olivia v so let's put them aside and really focus just on these two things now there's also another thing inside of note that is crucial to understand just like v8 in web uv and that is called event loop now event loop isn't another package or library that node.js has inside of its dependencies it's something that node has implemented with the use of v8 itself and loop uv and all of that c plus plus code that we saw that node.js has so what does this event loop do and exactly how does v8 event loop and lib uv kind of interact with each other to be able to get this node.js thing up and running so let's take a look at an example so let's say that we have this code two consonant lines and a for loop that's gonna run for at some time in between them let's see what's gonna happen when we run this code so here i have my console and node.js or v8 executes code in a synchronous manner meaning that it's going to start from the top and it's going to go all the way to the bottom of the file and execute every single line of code so that is happening inside of a single threat so here's what's going to happen it's going to start from the top it's going to see the content log and it's going to log first onto console then it's going to move into the for loop now this for loop is going to run for about how many is it for about a hundred million times so it's a huge for loop that's going to probably take a couple of minutes to complete or a couple seconds and what's happening inside of this for loop just imagine it might be some calculation that might take some amount of time maybe it's a hashing a string or modifying a string really doesn't matter the point is that this for loop is going to take a couple of seconds or a couple of minutes to complete so here's what's going to happen v8 is going to keep executing this for loop without even paying attention to this content lock so what happens is that my application would be blocked until this for loop finishes so it's going to keep executing this for loop over and over again for 100 million times and then once it's done it's going to move on to the last log and log second to the console so the important thing to understand here is that while this for loop is running my whole process my whole notice process would become unresponsive i can no longer go and fetch a fly from my storage or i can no longer respond to an http request or send requests to the outside world i can't even log a single string to the console so my whole process will become unresponsive until this for loop finishes now once you have set it up multiple node processes or you're running this for loop in kind of another thread which we're going to talk about your whole application will become unresponsive so your whole site would go down until this for loop finishes now definitely when you're running node.js in production you have got to scale it you have got to use some asynchronous techniques which we will talk about all of them in the course i know i have said that sentence for hundreds of times now but really you can't do that instead of a single process if you do your whole application would just stop working so this is what happens when you execute code synchronously now let me show you this example instead of my code editor as well so i have created a single app.js file on my desktop and i want to open up my code editor and also my terminal so what i'm going to do is to run that constant unlock first and then a for loop oops four let's i equals zero and then let's run it for i don't know 100 billion times so 100 billion times i guess we need another three yeah another three now we have 100 billion and i want to increment i by one and actually i'm not going to do anything inside of this for loop incrementing i by one for 100 billion times it's going to take a couple of seconds and then i'm going to put the last log right here so console.log second and i'm going to run this code and when i run it i'm quickly going to get this first command and i miss this r [Music] so i'm first going to get this first log onto my console and then it's going to wait for a couple of seconds and when it's waiting and it's doing this for loop my whole application will become unresponsive and this log is not going to run so here's what's going to happen i run it i get the first command and while it's doing that for loop if i take a look at my activity monitor and i search for node notice here that its cpu usage has jumped to a hundred percent and it cannot really do anything else it is running in a single threat now here if you take a look at the number of threats we have a couple of threats but we're going to talk about in just a minute why it's happening so my whole application is now unresponsive and i cannot do anything else unless this for loop finishes and v8 moves on to the next line of code okay so what does this exactly mean does this mean that node.js is this awful technology that could only do one thing at a time does this mean that when i want to read a file or respond to a request or fetch something from the database my whole application will become unresponsive well actually quite on the contrary nodejs is incredible at doing those things by allowing you to execute code asynchronously and that's possible by things like event loop and web uv so here's how it works for example you want to read a file you tell from v8 to loop uv and when loop that i'm going to do i'm going to read this file okay so you go ahead you little baby you go ahead and read the file fetch the file do all everything that you got to do and while you're doing those things i'm going to continue on with my code execution i'm going to continue in my v8 thing and execute the rest of the code and when you're done let me know somehow so that i could respond so that for example i could when you when you're done reading the file you give me the data and i might for example log into the console or i might give you another test to get that data and create a new file with it that probably doesn't make a good sense to you right now so let's get back to our diagram and let me show you exactly how does that work so here's our node process again we have v8 vbn loop and wv now let's say that we have this piece of code a console log and then reading a file and then a log after the file has been read and then final log which is the third lock now node is gonna start to execute this code from the beginning so it's gonna see the log and it's gonna log the first to the console then it's going to move on to the second line so here's where it gets interesting node.js is going to see this line of code that it says okay required dfs module and then read this file now when you're doing this when you're requiring the fs module and then calling this read file function if you recall we're actually requiring the fs.js file from the lib folder in the node.js source code and in that file we had those binding things those things that would kind of allow us to talk to the c plus plus face so node.js is gonna see this line and then it's gonna push an event to the event loop now this event loop is the stack you could push new things on it and it's going to keep running them in a loop until they're done and when it happens when node.js pushes something into event loop it's going to quickly continue on with the rest of the execution and it's going to log the third command now while the rest of the execution is undergoing the event loop is also doing its thing and the event loop is running these events one by one in a loop so it's constantly going to check to see if it has something on the stack if it has an event to attend to and if it does those events and it's done it's going to quit so node is going to push something into this event loop and then event loop is going to see okay i have to read a file so it's going to go ahead and talk to the lib uv it's going to tell the library here's the file that i need you to fetch here's the path and here's some more configuration for example and we might want to get it in a binary format or we're going to get it in a string format so we're going to tell olympus that go ahead and fetch the file so web uv is going to reach out to the operating system and then the operating system is going to go to the storage your hard drive and then fetch that file and then give it back to the node.js process give it back to the wv to process it and once libya v is done with processing it it's going to give it back to the event loop and then the event loop is going to see it and says okay the job is done whatever we have to do it had done it and now i'm ready to notify the source code that here's the data and you go ahead and do your own thing so it's going to give that data to this v8 and it's going to see it and execute this callback and now the event loop is done so we don't have we no longer have anything in the event loop so it's completely done and now dv8 is going to execute that callback like this and when it's done we're no longer going to have anything in the v8 and we also no longer have anything in the van loop so the node.js process is going to exit this is what we call an event-driven programming meaning that you're writing your code and then inside of that code you're kind of issuing different commands issuing different events for example here you're saying okay go ahead read this file and then you're going to continue on with the execution and then when those magic happen behind the scenes and then the file is fetched and read you're going to execute a callback responding back to that event now what are some of the things that we call an event actually there are lots and we're going to see a lot of them throughout the course reading files is one of them timer functions the http module doing things related to network in general and actually a lot more now let me show you a couple more examples to really try to solidify this knowledge so you have probably seen patterns like this so we might have a log function called a and then i want to say b c d and f let's just keep it at this and then i'm going to put one of these logs inside of a set timeout function like this set timeout constant log and for the time i'm going to specify zero if you're not familiar with the set timeout function we have a video dedicated to talking about these timer functions but basically it's going to see the code and execute it after some amount of time that i'm going to specify here in milliseconds for so for example if i say 1000 it's going to execute this constant log after 1000 milliseconds but what if i put zero what's going to happen in this case what do you think is going to happen in what order we're going to get these a b c d and f let's go and run it [Music] so node app.js when i run it i get a but notice that now instead of getting b i'm getting c d f and then at the end i want to get this b why do you think this happens the reason is when you're running a timer function like this you're actually calling the event loop you're putting something onto the event loop so your code is going to start to execute from line one it's going to see this timeout function and then node is going to push a new event onto the event loop after it sees this set timeout function and then it quickly going to continue on with the rest of the application and then once all of these are done the main execution is done it's going to execute the callback function relating to that event actually you have something dedicated for this purpose and note and that's called process dot process process.next tick [Music] and if i run this i want to get that same result a c d f and then at the end d b so this process the next trick what it does is that its only purpose is that if you want to run something after your main execution is done and it really comes in handy from time to time and we're going to see some of its use cases in the course but the point is when you're doing this you're pushing a new event onto the event loop and then the rest of your application is going to get run and then once done this line is going to run now also remember that we said for as long as we have something in the main thread in that v8 thing or in the event loop our node.js is going to run and whenever we have nothing in those two things node is going to exit so if i just have one content unlock and it's just one so something like this note it's quickly going to run it and then it's going to exit and i'm back at my desktop but if i do something silly like this so i say set interval [Music] and i just run this for hundreds now if you're not familiar with set interval what it does is that it executes this function every this amount of time so it's going to execute this code every 100 milliseconds and now i'm not going to put anything inside of it now if i run this code so node app.js it's not going to exit and it's going to keep going and going if i take a look at my activity monitor i search for node here i have it and it's not actually using any amount of cpu it's only using less than one percent of the cpu usage so what's happening right now i'm pushing something onto the event loop and i'm not exiting so as long as we have something in the event loop or the main thread that v8 nodejs is going to keep running and running now in the previous video when we created a simple web server that is exactly what's happening when you're listening for a specific port you're adding something onto the event loop and you're not actually deleting that event it's gonna keep being there until you hit ctrl c so the event loop is going to constantly watch out for new requests onto that port and whenever a new request comes to that port then we is going to get that and then it's going to tell event loop and event loop is going to call that callback function that is there to handle the requests now also it's very important that you never block the event loop or the v8s or the main threads so what i mean is if we take another look at this one this v8 thing and this event loop they are both running in a single thread so think of this little green rectangle as a threat and if you don't know what a threat is we're going to talk about it in a lot more great details later in the course but think of a threat as an execution line something that could only execute one thing at a time so for example in case of a pdf viewer if this application the speed of viewer is only running in a single thread and you search for something while the search is going you're not going to be able to for example highlight the text or go to the next page because the whole application is in a single thread when you occupy that thread when you kind of block it by that searching thing the whole application will become unresponsive and you could fix it by setting up multiple threads so one thread might handle the searching thing and another thread might handle the rest of the application so here we have the v8s and the event loop in a single thread so if you block each one of these your whole application will be unresponsive now and notice we have a couple more threads in the loop uv and are called thread pull and by default we have four threads right there and these handle some things like the file system so when you want to create a file or handle files in general this thread pull would kick in and handle that thing now as i said by default there are four of these but you could increase them using a flag in your terminal again don't worry too much about thread pull and this threading thing we have a whole discussion talking about these things like clustering child processes setting up multiple threads and a lot more now not all of the other tasks that are assigned to the slug uv are done using thread pull for example network requests aren't done using threadpool actually nodejs is doing its best to minimize the use of this thread pull for as much as possible so whenever it can it will reach out to the operating system and do things asynchronously so when you for example listening for requests the requests would coming through the internet through your network card and then it goes from the operating system straight to wv and without touching this thread pull it's going to go straight to the event loop and then v8 but some things are handled using this thread pool like dealing with files and this thing that nodejs is using trying to minimize the use of threats is what that makes node.js incredibly scalable and incredibly fast because it uses very little amount of threats so you could really scale it well without using a lot of hardware resources now again we're going to talk in much greater details about what not to do to block this thread pull or to block this main thread but let me just show you one example and illustrate what i mean when i say you you should never block the event loop or the main thread you should always watch out for those things so i'm going to go back to my code editor and here i'm going to set up a set timeout function [Music] and this timeout function is going to run after let's say 50 milliseconds like this and i'm just gonna do simple log so constant log done now if i run this you expect what's going gonna happen after a very short amount of time i'm gonna get down almost instantly i don't even have to count so as soon as i run it i'm gonna get this done and remember again when we're running this code as soon as node.js sees this timer function the set timeout function it's going to push a new event onto the event loop stack and then it quickly going to continue on with the rest of the code execution so here's rest the code and [Music] the event loop is gonna see this line push that event and then quickly continue on with the rest of the code and after about 50 milliseconds we expect to see this line kick in but what if i do something like this so i'm going to put in a for loop so four let i starting from zero going to let's say one billion so one triple zero triple zero it's one million another triple zero and now it's one billion i plus plus [Music] now what do you think would happen in this case do you think that node.js is gonna continue with this execution and after 50 milliseconds is going to move on to this line or do you think that the whole application would just stop what what is really a guess here so think about it for a minute okay so let me now execute this program and see what i'm going to get now remember that previously when we ran this node app.js we instantly got this done message now what's going to happen in this case so i'm going to say node.js and if i run it notice that it took a little bit longer amount of time before i get the done message to make it a little bit more clear i'm going to increase this number to 9 billion let's run again and i'm going to change it to only one millisecond so as soon as i run this node app.js i should instantly without even waiting for the shortest amount of time i should get this done message so let's see what's going to happen as you can see my application is now blocked so when node.js is executing this for loop and notice that now i got this done message but a lot longer after only one millisecond was a couple of seconds so what happens in this case is that i'm blocking that threat so the threat is now occupied it's constantly trying to increment this eye by one so that whole thread will be blocked and this event loop thing cannot even kick in so if i'm for example doing some heavy calculations you're using that thread pull like dealing with a file until this for loop is done i'm not going to be able to execute that line this is what we mean when we say do not block the main threads or the event loop and we're going to again talk about it in a lot more greater details later on in the course now if you go to the node.js documentation so i'm going to nodejs.org website and then i'm going to go to the docs section and right here i'm going to click on the guides if i scroll down a little bit in the node.js core concept section you will find a very very crucial article i can't really say enough how important this article is it says don't block the event loop or the worker pool aka the threat pool so this article is a little bit more advanced and definitely i don't expect you to come here and read it and understand it but we will of course talk a lot more about this it actually discusses in great details about what we talked about how not to block the event loop or the thread pull now here for example it is talking about some of the vulnerable regular expressions that if you run for example an attacker might be able to give it something that is going to occupy your whole process and your whole application will become unresponsive there are some important things that you need to watch out for to avoid attacks like denial of service and a lot more things that you need to really watch out for for example there are some things like the synchronous functions it's going to be way better to instead of using this sync functions that are doing that thing synchronously use the async versions and again we will talk about all of these in great details here for example you're seeing some complex calculations examples like partitioning using a for loop and yeah here it says some examples about offloading again i don't really expect you to understand anything about these but i just wanted to tell you that there's this very important article in node.js and we will come back to this later on okay that was about this long video and i know that i threw a lot of information at you we talked about javascript engines v8 some c plus plus assembly wv event driven programming asynchronous code and more and honestly i don't expect you to fully understand everything that we talked about here this video wasn't really meant to teach you things like the ybv itself or the v8 or c plus rather to give you feel of what node.js really is and hopefully by now you're not saying that notice is another javascript library or framework like react angular v or jquery nodejs is a lot more than that those libraries are just javascript they're written in it node.js understands javascript it's a lot more than just javascript it has added a lot of new features to the language in ways that we talked about now again don't worry if you didn't fully understand some of the things that we talked about we will definitely talk about things like devoid uv asynchronous code how not to block the application and a lot more later on in the course we also have to talk about scaling and important things like security clustering and so much more and truth be told i really oversimplified some of the topics we talked about here like that diagram with hardware and web uv and that v8 thing inside of it we will come back to that thing and make it more accurate when the time is right when we're talking about some specific things we will make that more accurate and make it better and better [Music] so we have a lot of things to do and a lot of things to learn so let's keep going i'll see you in the next one
Info
Channel: Cododev
Views: 12,133
Rating: undefined out of 5
Keywords: Node.js, C++
Id: 8fPdzhLPhr0
Channel Id: undefined
Length: 98min 39sec (5919 seconds)
Published: Wed Feb 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.