Bun, Deno, Node.js? Recreating a JavaScript runtime from Scratch - Understand magic behind Node.js

Rate this content
Bookmark

Bun, Deno, and many other JavaScript runtimes have been hyped, but do you know why? Is it that easy to make a runtime from scratch?

I've been researching the secret behind Node.js' power and why there are so many new JavaScript runtimes coming up. Breaking down each key component used on Node.js I've come to interesting conclusions that many people used to say whereas in practice it works a bit differently.

In this talk, attendees will learn the concepts used to create a new JavaScript runtime. They're going to go through an example of how to make a JavaScript runtime by following what's behind the scenes on the Node.js project using C++. They'll learn the relationship between Chrome's V8 and Libuv and what makes one JavaScript runtime better than others.

This talk will cover the following topics:
- What's a JavaScript Engine - V8
- Why Node.js uses Libuv
- How to create a JS Runtime from scratch

FAQ

The tutorials discussed in the talk are available online. Links to these resources are provided at the end of the talk, and they cover the detailed steps involved in the experiments and re-implementations discussed.

The speaker was inspired to start these experiments by a curiosity about how Node.js works behind the scenes, particularly after finding conflicting information and a lack of comprehensive resources on the subject.

The key components discussed include V8, which handles JavaScript execution; libuv, which manages asynchronous operations; and the C++ bridge, which integrates V8 with other system capabilities and custom functionalities.

The main focus of the experiments discussed in the talk is re-implementing various aspects of Node.js, including WebSockets and code coverage, using JavaScript and exploring the underlying technologies like V8 and libuv.

In the experiments, the speaker recreated aspects of Node.js, including a print function in V8 using C++, and the setTimeout function, showcasing the interaction between JavaScript and the system environment via C++.

The speaker encountered several challenges, including dealing with outdated tools, understanding complex C++ and system APIs, and integrating JavaScript code with low-level system operations.

The experiments are significant as they provide deep insights into the workings of JavaScript runtimes like Node.js, helping developers understand the complexities and capabilities of the platform, which can be valuable knowledge during job interviews or when optimizing applications.

Yes, the speaker mentions that all the content, tutorials, and further readings are available on their website. Additionally, an e-book detailing the experiments and findings is also available for those interested in a step-by-step guide.

Erick Wendel
Erick Wendel
29 min
14 Apr, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

The Talk explores the magic behind Node.js and delves into its components, including V8, libuv, and the C++ bridge. It discusses the workflow and execution process, the use of NodeMod, and the understanding of console functions. The Talk also covers Node.js functions and scheduling, the introduction of runtimes, and the collaboration between JavaScript runtimes. It concludes with insights on content production, the choice of Node.js, and the inspiration behind it.

1. Introduction to Node.js and My Research

Short description:

Today, I'm gonna talk to you about some experiments, some mad sciences I've been doing using JavaScript and a lot of stuff. I started researching about Node.js and found conflicting information. So I decided to create a tutorial based on my own research. Let's understand the magic behind Node.js and explore the Node.js repository.

Today, I'm gonna talk to you about some experiments, some mad sciences I've been doing using JavaScript and a lot of stuff, and I hope you really like this content because it was a real effort to create everything here. So to get started, everything that I'm gonna show you today is already online, so after all the talk I'm gonna show you some links so you can go there, but please, if you can, take a picture of this talk, mention the event, mention myself because this helps us a lot with the work we've been doing.

All right. I'm so excited, I'm gonna talk about Node.js and the NodeJS creator is here, the bun, and so on. So it's pretty amazing. Well, first of all, I've been doing a lot of other experiments. I was trying to re-implement Node.js, re-implementing WebSockets, re-implementing a code coverage, too, so I've been making a lot of specific questions, so I'm very curious, and all these tutorials are there so you can find it as well.

Well, all this experiment started when I started asking myself, well, do I really know what is Node.js? So I started researching, and I figured some posts were telling that V8 does one thing, Libv does another, JavaScript is another role, and sometimes one post was controversial to another, so I was like, hmm, maybe I should learn more, maybe I should understand better. So I don't really know how it's going behind the scenes, how it's really working. So I started researching a bit, and I figured out there's no content about this. No one has ever done recreating the whole stuff, compiling the whole libraries, but I started researching on the Node.js website, and those links helped a lot to learn how the event loop, how the concurrency module in Node.js was working, but still, I wanted more. So that's why I created this tutorial. This is a full step-by-step tutorial, which is this talk is based, so this talk is going to be a lot of highlights, because I cannot show all the hands-on right here. So you can try it out later.

Just a disclaimer, before we go on, I'm going to tell you, everything here is part of my research, okay? I'm not a C++ developer. You might see a lot of bad practices right there, but it's something that I was enjoying in doing. Also, this is part of my own research. As I tell you, there's no content on the internet. So I started asking some friends, looking at the source code and getting some assumptions. And just a heads out, JS Runtimers, our authors, are amazing. I started to give more value to them as I saw how complex is it behind are able to use JavaScript in there.

All right, let's go to the fun part, right? So let's understand the magic behind Node.js. So everything here, I did like a Gitpod. I did all the environment for you right there. It's binaries and a lot of stuff that you can start using right away. To get started, I was like, what if I go to the Node.js repo and try finding how Ryan Doll was doing this stuff? So I found out a lot of files. And I found out like, oh, maybe I should try reproducing this, but if you see, it's 14 years ago. Like a lot of tools, it's not even working anymore. But still, has anyone seen this website before? No one. This is so nice.

2. Introduction to Node.js Components

Short description:

This was the first version released of the Node.js. It's the V001. And you can see there was no console.log back then. It was puts. Very nice. I tried to split the main components so you can understand each individual role. We're going to talk about V8, libuv, and the mind-blowing C++ bridge. I'm gonna try implementing a new feature on the V8 side. Let's take a look at our JS code and create a print function on C++. Behind the scenes, V8 is like the evolver. A setTimeout function is something that is asynchronous, it depends on the environment. This is why Node.js is so good, because it's extensible. Most of the JS run times follow the same idea. I'm gonna try doing some experiments using our JavaScript code. Here's the whole project in C++.

This was the first version released of the Node.js. It's the V001. And you can see there was no console.log back then. It was puts. Very nice.

All right. I know this is a very complex subject, so I won't make you sleep right here, okay? So I tried to split the main components so you can understand each individual role. If you are trying to search for jobs, this is kind of cool to tell on the interview.

So first of all, we're going to talk about V8. V8 is grammar, it's the JavaScript data types, it's how JavaScript is interpreted, which means a class, a variable, a data type, it's all on V8. We also have the libuv. Libuv is the async thing that we've been listing a lot. But just think of it as a while true asking for new events, and, if so, if there are any pinned events, it's going to dispatch them all and you can start receiving more data and so on.

And here, for me, is the mind-blowing part. The C++ bridge. So when you try finding, you're gonna realize, Node.js is almost everything on C++. I'm gonna try a magic with you, trying to implement a new feature on the V8 side. So let's take a look at our JS code. When you're starting using the V8 from scratch, our context, our global disk is empty. So there's nothing there that we can use, but I'm gonna try implementing the print function. Print doesn't exist in JavaScript, all right? So if I want to be able to execute this function from the JavaScript side, this must be on the V8. So using the C++ bridge, I'm gonna create a print function on C++, and then I'm gonna bind it to the context. See, I would say, every time I see this string, I'm gonna call this C++ function. Behind the scenes, V8 is like the evolver, right? It's evaluating everything that you want.

Okay, let's go try doing something harder. A setTimeout function is something that is asynchronous, it depends on the environment, so we can use the uvstart, which is leave uv functions. We do exactly the same thing, we map this string to this C++ function and then it's already available on the V8. I'll tell you, this was the part for me like, oh my god, this is why this is so good, because this is extensible, right? Most of the JS run times follow the same idea, they are extending the JavaScript runtime environment and do a lot of cool stuff. So right here, I'm gonna try doing some experiment using our JavaScript code. So here, I can see Index.js file and here's the whole project in C++.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Node Congress 2022Node Congress 2022
26 min
It's a Jungle Out There: What's Really Going on Inside Your Node_Modules Folder
Top Content
Do you know what’s really going on in your node_modules folder? Software supply chain attacks have exploded over the past 12 months and they’re only accelerating in 2022 and beyond. We’ll dive into examples of recent supply chain attacks and what concrete steps you can take to protect your team from this emerging threat.
You can check the slides for Feross' talk here.
Towards a Standard Library for JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
Towards a Standard Library for JavaScript Runtimes
Top Content
You can check the slides for James' talk here.
ESM Loaders: Enhancing Module Loading in Node.js
JSNation 2023JSNation 2023
22 min
ESM Loaders: Enhancing Module Loading in Node.js
Native ESM support for Node.js was a chance for the Node.js project to release official support for enhancing the module loading experience, to enable use cases such as on the fly transpilation, module stubbing, support for loading modules from HTTP, and monitoring.
While CommonJS has support for all this, it was never officially supported and was done by hacking into the Node.js runtime code. ESM has fixed all this. We will look at the architecture of ESM loading in Node.js, and discuss the loader API that supports enhancing it. We will also look into advanced features such as loader chaining and off thread execution.
How Bun Makes Building React Apps Simpler & Faster
React Day Berlin 2022React Day Berlin 2022
9 min
How Bun Makes Building React Apps Simpler & Faster
Bun’s builtin JSX transpiler, hot reloads on the server, JSX prop punning, macro api, automatic package installs, console.log JSX support, 4x faster serverside rendering and more make Bun the best runtime for building React apps
Out of the Box Node.js Diagnostics
Node Congress 2022Node Congress 2022
34 min
Out of the Box Node.js Diagnostics
In the early years of Node.js, diagnostics and debugging were considerable pain points. Modern versions of Node have improved considerably in these areas. Features like async stack traces, heap snapshots, and CPU profiling no longer require third party modules or modifications to application source code. This talk explores the various diagnostic features that have recently been built into Node.
You can check the slides for Colin's talk here. 
Node.js Compatibility in Deno
Node Congress 2022Node Congress 2022
34 min
Node.js Compatibility in Deno
Can Deno run apps and libraries authored for Node.js? What are the tradeoffs? How does it work? What’s next?

Workshops on related topic

Node.js Masterclass
Node Congress 2023Node Congress 2023
109 min
Node.js Masterclass
Top Content
Workshop
Matteo Collina
Matteo Collina
Have you ever struggled with designing and structuring your Node.js applications? Building applications that are well organised, testable and extendable is not always easy. It can often turn out to be a lot more complicated than you expect it to be. In this live event Matteo will show you how he builds Node.js applications from scratch. You’ll learn how he approaches application design, and the philosophies that he applies to create modular, maintainable and effective applications.

Level: intermediate
Build and Deploy a Backend With Fastify & Platformatic
JSNation 2023JSNation 2023
104 min
Build and Deploy a Backend With Fastify & Platformatic
WorkshopFree
Matteo Collina
Matteo Collina
Platformatic allows you to rapidly develop GraphQL and REST APIs with minimal effort. The best part is that it also allows you to unleash the full potential of Node.js and Fastify whenever you need to. You can fully customise a Platformatic application by writing your own additional features and plugins. In the workshop, we’ll cover both our Open Source modules and our Cloud offering:- Platformatic OSS (open-source software) — Tools and libraries for rapidly building robust applications with Node.js (https://oss.platformatic.dev/).- Platformatic Cloud (currently in beta) — Our hosting platform that includes features such as preview apps, built-in metrics and integration with your Git flow (https://platformatic.dev/). 
In this workshop you'll learn how to develop APIs with Fastify and deploy them to the Platformatic Cloud.
0 to Auth in an Hour Using NodeJS SDK
Node Congress 2023Node Congress 2023
63 min
0 to Auth in an Hour Using NodeJS SDK
WorkshopFree
Asaf Shen
Asaf Shen
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.JS backend + React frontend) to authenticate users with OAuth (social login) and One Time Passwords (email), including:- User authentication - Managing user interactions, returning session / refresh JWTs- Session management and validation - Storing the session for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.
Table of contents- A quick intro to core authentication concepts- Coding- Why passwordless matters
Prerequisites- IDE for your choice- Node 18 or higher
Building a Hyper Fast Web Server with Deno
JSNation Live 2021JSNation Live 2021
156 min
Building a Hyper Fast Web Server with Deno
WorkshopFree
Matt Landers
Will Johnston
2 authors
Deno 1.9 introduced a new web server API that takes advantage of Hyper, a fast and correct HTTP implementation for Rust. Using this API instead of the std/http implementation increases performance and provides support for HTTP2. In this workshop, learn how to create a web server utilizing Hyper under the hood and boost the performance for your web apps.
Build Peer-to-Peer Applications with Pear Runtime
JSNation 2024JSNation 2024
152 min
Build Peer-to-Peer Applications with Pear Runtime
WorkshopFree
David Mark Clements
David Mark Clements
Learn how to rapidly build peer-to-peer applications with Pear Runtime. No servers required. Understand peer-to-peer paradigms and construct applications from well-defined building blocks. This workshop will cover how to create both Desktop and Terminal applications (with discussion for Mobile) that work entirely peer-to-peer from anywhere in the world. By the end of this workshop you should know how to build a new type of highly scalable application with entirely reduced infrastructural costs (~0) along with suitable architectures and best practices for peer-to-peer applications. From the creator of Pear Runtime and the company that brings us keet.io. Table of content:- Introducing Pear- Initial Q & A- Getting Setup- Creating a Pear Desktop Application- Sharing a Pear Application- Running a Pear Application- Creating a Pear Terminal Application- Releasing a Pear Application- Architectural Discussions- Wrap-up Q & A
GraphQL - From Zero to Hero in 3 hours
React Summit 2022React Summit 2022
164 min
GraphQL - From Zero to Hero in 3 hours
Workshop
Pawel Sawicki
Pawel Sawicki
How to build a fullstack GraphQL application (Postgres + NestJs + React) in the shortest time possible.
All beginnings are hard. Even harder than choosing the technology is often developing a suitable architecture. Especially when it comes to GraphQL.
In this workshop, you will get a variety of best practices that you would normally have to work through over a number of projects - all in just three hours.
If you've always wanted to participate in a hackathon to get something up and running in the shortest amount of time - then take an active part in this workshop, and participate in the thought processes of the trainer.