JavaScript Runtime Demystified!

JavaScript Runtime Demystified!

Before we dive into understanding runtime, we need to talk about how NodeJS was born. But in order to talk about how Node got started, why it's so big, why it's everywhere, and why you need to learn it. Well, we need to ask this question -

??How do you run JavaScript?

If I told you, hey, run this piece of JavaScript code for me. How would you do it? Would you be able to run JavaScript on any computer? Let's say you bought a brand-new computer. Can you run JavaScript on it?

Well, for some of you, your first answer might be, well, yeah, I can run it in the browser. If we go to our browser, let's say you're using Google Chrome, Firefox, whatever, you can open up the developer tools and you can run JavaScript in your console, right? But the thing that's running JavaScript for you here is now your computer. I mean, yes, it's your computer, but it's the browser that you downloaded or came pre-installed on your computer when you bought it. That allows us to run JavaScript. If we had a fresh computer with nothing installed, Well, JavaScript would be meaningless to the computer, right?

Hence, we arrived at the conclusion that in order to run JavaScript on our computer without using a browser or outside of the browser, we need a tool that can read and understand JavaScript, then convert it to something that a computer can understand. And this is what a JavaScript engine does.

??What is a JavaScript Engine?

When we download, let's say, Google Chrome, it comes with a built-in JavaScript engine. This JavaScript engine takes whatever we write over here and understands it and runs the code for us. let's do a quick Google search here.

No alt text provided for this image

Well, as you can see here, a JavaScript engine is a computer program that executes JavaScript code. It reads our job script and it executes. So if I open up JavaScript engines and Wikipedia, are there multiple JavaScript engines -

No alt text provided for this image

For example, Google Chrome uses something called the V8 JavaScript Engine.

But hold on there, We didn't really answer the question, are we able to run JavaScript outside of the browser and this is where NodeJS comes in.

??The birth of the NodeJS runtime

The release of Chrome's highly performant JavaScript engine, V8, revolutionized the browsing experience and contributed to the widespread adoption of Chrome browsers. This engine also served as the foundation for NodeJS, which Ryan Dahl developed in 2009 with the goal of enabling the use of V8 outside of the browser.

By breaking free from the limitations of the browser environment, NodeJS opened up new possibilities for developers and facilitated the emergence of full-stack development. With NodeJS, JavaScript developers could now leverage their skills to work not just on the web but also in the realm of back-end servers and microservices.

The ability to use a single programming language, JavaScript, for both client-side and server-side development has been a game-changer for the industry and has led to a significant rise in the demand for full-stack developers. NodeJS continues to be a crucial tool for developers, enabling them to build powerful, scalable, and versatile applications across a range of domains.

We know what Chrome's V8 JavaScript engine is, but what is a runtime? Let's learn about that in the next section.

??What is a Runtime?

You might hear that Node is a JavaScript runtime. It's not a programming language, it's not a framework. It's a runtime. So let's demystify that.

You can think of the runtime as a JavaScript environment, an environment that allows us to run JavaScript as well as do some extra stuff. Now the way Node works is that it has the V8 engine, which, as we learned, makes our JavaScript code run really efficiently. And every time Google engineers upgrade the V8 engine, which is very often well known, it automatically gets faster and faster with each improvement in the V8 engine. So automatic performance boost just by that.

But in order for it to do a lot more than just read JavaScript, it might want to do different things - Maybe read files, maybe access databases, maybe just run some things that aren't part of the V8 engine aren't part of JavaScript. And that's where the libuv comes in.

No alt text provided for this image

You see, a runtime in a simplified form is simply a way for us to give a JavaScript file to the V8 engine. It's going to go through the V8 engine. It's going to read JavaScript. And any time it sees something that maybe isn't part of JavaScript, it's going to communicate with this libuv library with a cool dinosaur logo. But what does it mean? Well, We can just simply Google libuv.

No alt text provided for this image

It's essentially a library that allows us to do some really neat features with Node.js that make it great for things like web servers. But essentially, what Node and Ryan Dahl did was combine the V8 engine and this libuv, which is written in C++, V8 engine is written with JavaScript and C++, and he created the bindings between them so that they can communicate so that whenever V8 encounters something that maybe needs asynchronous I/O, well, then V8 hand it over to the libuv.

No alt text provided for this image

Now, let me ask you another question -

Is a Web browser a JavaScript runtime?

Hold a few seconds and think about it based on what you learned is JavaScript runtime. Well, let's do a quick search on Google. Here is what I have found on StackOverflow

No alt text provided for this image

In a sense, yes, the web browser is a JavaScript runtime. If we go to our console, we can actually run code that isn't part of JavaScript. For example, console.log isn't actually part of the JavaScript Spec. It's just something that the browsers allow us to do things like alert.

No alt text provided for this image

Well, those are very Web specific, the browser implements this alert feature that we can use and all of these are part of what we call the window object in the browser,

No alt text provided for this image

the window object is a set of tools that the browsers give us that we can use that aren't part of JavaScript but allow us to have multiple functionalities. So it's a JavaScript runtime.

Remember the V8 engine combined with some extra functionality, in the same sense, in Node.js, we have that functionality, except because it's not a browser. If you do window, you're going to get an error.

No alt text provided for this image

In NodeJS, we have something called global instead of the window object. But if I do global here in the browser console, I get an error because the browser doesn't know about global.

No alt text provided for this image

This is one of the main differences between NodeJs and a browser.

In summary, to run JavaScript, You need some sort of a JavaScript engine and a runtime to be able to run the code, and that's what NodeJS does. So how do you run JavaScript outside of the browser? You need to download a set of features so that your computer understands what JavaScript is, and what you want to do.

No alt text provided for this image

NodeJS is, in a way, just a set of tools for us to write our applications. That application uses the V8 engine to read the JavaScript, and any time there's something that requires some sort of things like worker threads, event queues, or event loops, then we use the libuv Library and Ryan Dahl created these bindings(node APIs) for us that communicates between V8 and libuv.

So then now we're able to write JavaScript gets read by the V8 engine and then gets sent back to libuv. But we don't have to worry about what's underneath the hood. We can just run JavaScript code.

要查看或添加评论,请登录

??Prasenjit Sutradhar的更多文章

社区洞察

其他会员也浏览了