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

This talk has been presented at Node Congress 2023, check out the latest edition of this JavaScript Conference.

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.
  • ff
    ff
    test
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
The talk discusses the importance of supply chain security in the open source ecosystem, highlighting the risks of relying on open source code without proper code review. It explores the trend of supply chain attacks and the need for a new approach to detect and block malicious dependencies. The talk also introduces Socket, a tool that assesses the security of packages and provides automation and analysis to protect against malware and supply chain attacks. It emphasizes the need to prioritize security in software development and offers insights into potential solutions such as realms and Deno's command line flags.
Towards a Standard Library for JavaScript Runtimes
Node Congress 2022Node Congress 2022
34 min
Towards a Standard Library for JavaScript Runtimes
Top Content
There is a need for a standard library of APIs for JavaScript runtimes, as there are currently multiple ways to perform fundamental tasks like base64 encoding. JavaScript runtimes have historically lacked a standard library, causing friction and difficulty for developers. The idea of a small core has both benefits and drawbacks, with some runtimes abusing it to limit innovation. There is a misalignment between Node and web browsers in terms of functionality and API standards. The proposal is to involve browser developers in conversations about API standardization and to create a common standard library for JavaScript runtimes.
ESM Loaders: Enhancing Module Loading in Node.js
JSNation 2023JSNation 2023
22 min
ESM Loaders: Enhancing Module Loading in Node.js
ESM Loaders enhance module loading in Node.js by resolving URLs and reading files from the disk. Module loaders can override modules and change how they are found. Enhancing the loading phase involves loading directly from HTTP and loading TypeScript code without building it. The loader in the module URL handles URL resolution and uses fetch to fetch the source code. Loaders can be chained together to load from different sources, transform source code, and resolve URLs differently. The future of module loading enhancements is promising and simple to use.
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 is a modern all-in-one JavaScript runtime environment that achieves new levels of performance. It includes BUN dev, a fast front-end dev server, BUN install, a speedy package manager, and BUN run, a fast package runner. BUN supports JSX, has optimized React server-side rendering, and offers hot module reloading on the server. The priorities for BUN include stability, node compatibility, documentation improvement, missing features in BUN install, AST plugin API, native Windows support, Bundler and Minifier optimization, and easier deployment to production. BUN's AST plugin API allows for bundle-time JavaScript execution and embedding code, potentially inspiring new frameworks.
Out of the Box Node.js Diagnostics
Node Congress 2022Node Congress 2022
34 min
Out of the Box Node.js Diagnostics
This talk covers various techniques for getting diagnostics information out of Node.js, including debugging with environment variables, handling warnings and deprecations, tracing uncaught exceptions and process exit, using the v8 inspector and dev tools, and generating diagnostic reports. The speaker also mentions areas for improvement in Node.js diagnostics and provides resources for learning and contributing. Additionally, the responsibilities of the Technical Steering Committee in the TS community are discussed.
Node.js Compatibility in Deno
Node Congress 2022Node Congress 2022
34 min
Node.js Compatibility in Deno
Deno aims to provide Node.js compatibility to make migration smoother and easier. While Deno can run apps and libraries offered for Node.js, not all are supported yet. There are trade-offs to consider, such as incompatible APIs and a less ideal developer experience. Deno is working on improving compatibility and the transition process. Efforts include porting Node.js modules, exploring a superset approach, and transparent package installation from npm.

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.
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.
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
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.