JavaScript runtimes extend JavaScript's capabilities by bridging with languages like C++ and Rust.
The differences in runtime performance stem from how data is handled and communicated with the operating system.
Node.js remains relevant due to its active community and ongoing performance improvements.
Node.js uses V8, C++, and libuv to execute JavaScript, extending its functionality with custom implementations.
Understanding low-level implementations enhances development skills and offers insights into runtime functionality.
Embarking on a journey to understand the inner workings of JavaScript runtimes offers a unique perspective on how modern web development tools operate. Exploring the core components and mechanisms of runtimes like Node.js, Dino, and Bun reveals the intricate dance between JavaScript, C++, and other languages. This exploration not only enhances our technical skills but also deepens our appreciation for the technologies we often take for granted.
JavaScript runtimes such as Node.js serve as an interface between JavaScript and lower-level languages like C++. They allow developers to execute JavaScript code by compiling it into machine-readable instructions and extending its capabilities with additional functions. Node.js, for instance, uses V8 as its engine to interpret JavaScript and leverages C++ to bridge interactions with the operating system. This setup enables JavaScript developers to perform tasks that typically require lower-level programming expertise.
The emergence of new JavaScript runtimes like Dino and Bun has ignited discussions around performance and developer experience. These runtimes offer different approaches to handling data and interactions with the operating system, leading to variations in speed and efficiency. Dino, for example, uses TypeScript and Rust to compile JavaScript into executable code, aiming to enhance security and developer productivity. Similarly, Bun employs JavaScript core to optimize data handling and execution speed.
One key aspect that sets these runtimes apart is how they manage file system APIs and data buffers. The efficiency of data transfer between the runtime and the operating system can significantly impact performance. For instance, Bun claims to outperform Node.js by utilizing alternative programming languages and techniques to streamline data processing. As developers, understanding these differences helps us make informed choices when selecting a runtime for our projects.
Despite the growing competition, Node.js continues to thrive due to its robust community and ongoing improvements. The introduction of a performance working group has led to significant speed enhancements in recent versions, demonstrating the project's commitment to staying relevant. Node.js's extensive ecosystem and adherence to ECMAScript specifications make it a reliable choice for many developers, even as new runtimes emerge.
Behind the scenes, Node.js relies on several key components to execute JavaScript code. V8, the JavaScript engine, transforms code into a format that can be understood by the machine. C++ acts as a bridge, facilitating communication between JavaScript and the operating system. Libuv, another critical component, handles asynchronous operations and event loops, enabling Node.js to manage tasks like file I/O and networking efficiently.
Exploring the foundational elements of Node.js reveals the complexity involved in creating a runtime from scratch. By delving into the source code and understanding the interactions between different components, developers can gain valuable insights into how JavaScript runtimes function. This knowledge not only enhances our technical proficiency but also empowers us to contribute to the development of new tools and technologies.
The process of extending JavaScript's functionality involves injecting custom implementations into the runtime's environment. For example, functions like setTimeout and setInterval are implemented in C++ and exposed to JavaScript through the runtime. This approach allows developers to create powerful applications using familiar JavaScript syntax while leveraging the performance benefits of lower-level languages.
By experimenting with runtime implementations and exploring the source code, developers can uncover the nuances of JavaScript execution. This hands-on experience provides a deeper understanding of how runtimes optimize code execution and manage resources. It also highlights the importance of collaboration between different runtime projects, as developers from Node.js, Dino, and Bun often share knowledge and contribute to each other's work.
In the ever-evolving landscape of web development, understanding the inner workings of JavaScript runtimes is a valuable skill. It equips developers with the knowledge needed to navigate the complexities of modern software development and make informed decisions about the tools and technologies they use. By embracing this exploration, we can unlock new possibilities and contribute to the continued evolution of the JavaScript ecosystem.
Bun, Deno, and many other JavaScript runtimes have been hype, 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 has been presented at JSNation 2023, check out the latest edition of this JavaScript Conference.
The speaker suggests extending the V8 engine by implementing custom functions in C++ and linking them to JavaScript calls, effectively adding new functionalities like 'print' and 'setTimeout' to the V8 global context.
The speaker faced significant challenges, including the complexity of understanding and implementing the various components of Node.js, coordinating with experts, and the time-intensive nature of compiling and managing the required environments and libraries.
The speaker encourages taking pictures and mentioning the event on social media to help promote their work and extend the reach of the event, aiding in community building and recognition.
The key takeaway was the realization that Node.js extends functionalities by linking JavaScript with C++ operations, demonstrating that much of what seems complex is manageable with the right understanding and tools.
The speaker mentions an upcoming project to recreate React Native, indicating plans to explore and understand another complex framework by breaking it down into its fundamental components.
The speaker was motivated by a fear of being asked detailed questions about Node.js's inner workings at conferences and realized a gap in his knowledge. This led him to deeply explore and recreate Node.js to understand it better.
In the reconstruction of Node.js, the main components used were JavaScript for scripting, the V8 engine for JavaScript interpretation, C++ as a bridge for connecting JavaScript with the operating system, and libuv for managing event loops and asynchronous operations.
The tutorial and all related resources, including slides and links, are provided in the last slides of the presentation discussed in the talk.
This Talk explores the journey of learning and recreating Node.js from scratch, highlighting the main components and experimentation involved. It delves into implementing functions in V8 and setTimeout in Node.js, as well as the execution pipeline and the event loop. The collaboration between different JavaScript runtimes and the continuing evolution of Node.js are also discussed. The speaker shares their experience of exploring Node.js and writing a book, and hints at future projects involving React Native.
1. Introduction to Node.js and My Journey of Learning
Hello, everyone. We're gonna make some crazy experiments today. I tried recreating Node.js from scratch and I'm going to tell you all the step-by-step. First of all, all the slides, all the links, references and even a tutorial for you to follow after this conference I put on the last slides. Mention me, mention the event because this is how you can push our job further. Well, all this talk is about, do I really know Node.js? So I start researching a lot of things and start making questions. I've been creating a bunch of videos doing the same ideas. But this talk, I already have a tutorial there and here today I'm going to show you some highlights, and I even wrote a blog post for you later to extend.
2. Introduction to Node.js and My Journey
I'm not a C++ developer, but I'm curious and eager to learn. This research is my own, and I had to reach out to Google for help. Shout-out to Ben, Dino, Node.js, and the creators for their amazing job. They abstract the complexity, allowing us to create powerful applications. Let's do some magic!
3. Node.js Components and Experimentation
I tried finding GitHub projects, but they only did V8 and never used all the components. I wanted to experiment and went to the Node.js website, but it wasn't enough. I found the first Node.js version on GitHub and realized it was just a proof of concept. Now let's talk about the main components: JS, V8, C++, and libuv. The event loop is the magic in libuv, and the C++ bridge connects everything. If we have a JS file with just 'print', it won't work.
4. Implementing Functions in V8
When you compile V8 from scratch, the global list of JavaScript is empty. We can extend V8 on the C++ side and implement functions using annotations of V8. This allows us to call C++ functions from JavaScript.
5. Implementing setTimeout in Node.js
On the setTimeout side, it's exactly the same thing. I have to create the C++ function now, and then I can use uvtimerstart, just to reference that we are using libuv to create and to call those functions. I'm injecting a function on the global state, now the global disk has the setTimeout, and the V8 now thinks that setTimeout was always there. It's pretty great how Node.js is working. If you want to create your runtime, you've got to focus on creating your own implementations.
6. Execution Pipeline and Calling console.log
Let's explore the execution pipeline of Node.js and how it works behind the scenes. We start by reading a file, compiling the JavaScript code into C++ instances, and running the code. Then, we wait for events in the event loop. It's interesting to note that calling console.log relies on the environment and is not part of JavaScript itself.
7. Surprises with setTimeout and setInterval
But if I try printing the console, I'm going to see there's an object there. Pretty weird. If I use object keys, I'm going to see all the console API, all the methods, they are there, but they are just not useful. What if we try using setTimeout? Exactly the same problem. SetTimeout doesn't exist. So Node.js is injecting a bunch of stuff in V8. If you want to have your own browser on your runtimes, there are other working groups that can advise you how you could implement your own functions. On ECMAScript, we have all the grammar and all the things how JavaScript should be executed. Let's try using this print and inject on the V8 context so we can see it working in practice. We can use date, strings, and all the complex objects in JS. Promises are part of JavaScript, but they are there just to be wrappers for callbacks. Well, let's try implementing now this setTimeout.
8. Linking Elements with libuv and C++
To link both elements, we create a function using libuv. We check if it's a callback function and use a getter structure in C++ to save the variables for future execution. We use uw timer, a libuv call, and when the timeout ends, we call the callback function. There's no JavaScript here, just C++. It's amazing to think about embeddables and other cool concepts.
9. Event Loop and Extending V8
The event loop, waitForEvents, and extending V8 are straightforward. By compiling and executing the code, the timeout becomes available on the JavaScript side. Abstractions for setInterval and setTimeout are created using promises and async await, eliminating the need for callbacks. Everything follows the ECMAScript specification.
10. JavaScript Runtimes and Collaboration
My friends, I cannot tell you how was my feeling when I saw those logs working for the first time. Node.js has a bunch of methods. They expose some APIs that are on the C++ side and you can handle from JavaScript. Why do we have so many JavaScript runtimes appearing now? Dino, Bun, and Node.js are doing the same thing but handling data differently. Developer experience matters. There is no competition, as developers from different runtimes collaborate together. Go to the tutorial and see for yourself.
QnA
Continuing with Node.js and Bun
If you want to see more content, go to my website. I built an ebook for you and put a challenge to implement your own FS red file. Node.js has a great community and continues to evolve. It's still fast compared to newer runtimes. Bun is said to be faster than Node.js, but the programming language they chose is different - Zig.
Exploring Node.js and Writing a Book
I found the time to explore Node.js and write a book because it was a good opportunity for me to learn and create unique content. Despite the delays and challenges, it was worth it in the end. I'm planning to work on a React Native clone, which will be a complex and exciting project.
Today's Talk discusses the importance of managing technical debt through refactoring practices, prioritization, and planning. Successful refactoring requires establishing guidelines, maintaining an inventory, and implementing a process. Celebrating success and ensuring resilience are key to building a strong refactoring culture. Visibility, support, and transparent communication are crucial for addressing technical debt effectively. The team's responsibilities, operating style, and availability should be transparent to product managers.
Debugging JavaScript is a crucial skill that is often overlooked in the industry. It is important to understand the problem, reproduce the issue, and identify the root cause. Having a variety of debugging tools and techniques, such as console methods and graphical debuggers, is beneficial. Replay is a time-traveling debugger for JavaScript that allows users to record and inspect bugs. It works with Redux, plain React, and even minified code with the help of source maps.
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.
This Talk discusses building a voice-activated AI assistant using web APIs and JavaScript. It covers using the Web Speech API for speech recognition and the speech synthesis API for text to speech. The speaker demonstrates how to communicate with the Open AI API and handle the response. The Talk also explores enabling speech recognition and addressing the user. The speaker concludes by mentioning the possibility of creating a product out of the project and using Tauri for native desktop-like experiences.
React query version five is live and we'll be discussing the migration process to server components using Next.js and React Query. The process involves planning, preparing, and setting up server components, migrating pages, adding layouts, and moving components to the server. We'll also explore the benefits of server components such as reducing JavaScript shipping, enabling powerful caching, and leveraging the features of the app router. Additionally, we'll cover topics like handling authentication, rendering in server components, and the impact on server load and costs.
This Talk discusses various strategies to improve React performance, including lazy loading iframes, analyzing and optimizing bundles, fixing barrel exports and tree shaking, removing dead code, and caching expensive computations. The speaker shares their experience in identifying and addressing performance issues in a real-world application. They also highlight the importance of regularly auditing webpack and bundle analyzers, using tools like Knip to find unused code, and contributing improvements to open source libraries.
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
Shopify merchants have a diverse set of needs, and developers have a unique opportunity to meet those needs building apps. Building an app can be tough work but Shopify has created a set of tools and resources to help you build out a seamless app experience as quickly as possible. Get hands on experience building an embedded Shopify app using the Shopify App CLI, Polaris and Shopify App Bridge.We’ll show you how to create an app that accesses information from a development store and can run in your local environment.
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.
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.
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.
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
Comments