High Performance Node.js Powered by Rust and WebAssembly

Rate this content
Bookmark

In the post Moore’s Law era, due to limitations of the hardware, we need to squeeze more performance from the existing hardware. That means that the native code provides the best performance. However, the prevalence of native code on the server-side presents challenges to application safety and manageability. The rise and advent of Rust and WebAssembly offers new ways for developers to write high performance yet safe Node.js applications.


In this talk, I will cover the basics of Rust and WebAssembly, as well showcase how to go about their integration with Node.js. You will learn how and when to design a hybrid web application. How can you code the high performance functions in Rust in a Web Assembly virtual machine and finally how to tie everything together in a Node.js JavaScript application.

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

FAQ

Shivai Lamba is a TensorFlow.js special interest group member and a working group member from India. He is also a mentor for TensorFlow, specifically TensorFlow.js.

Shivai Lamba's talk at Node.js Congress 2022 focused on high performance Node.js powered by Rust and WebAssembly.

Rust is preferred because it provides safety, modernity, and efficiency comparable to C++, making it less error-prone and better suited for high-demand performance applications.

WebAssembly (Wasm) is a technology that allows code written in languages like Rust to be compiled into bytecode that runs on a virtual machine. It provides benefits such as security, portability, efficiency through techniques like ahead-of-time compilation, and runtime safety.

Integrating Rust and WebAssembly enhances Node.js applications by offloading highly computationally intensive tasks to Rust, which is managed within a WebAssembly-based virtual machine, resulting in improved performance and security.

To set up an environment for using Rust with Node.js and WebAssembly, you need a modern version of Ubuntu, Rust language, Node.js, Wasm Edge Runtime, and a Rust WebAssembly compiler toolkit.

Rust functions can be compiled into WebAssembly bytecode, which is then managed by a WebAssembly virtual machine. These functions can be called from a Node.js application using JavaScript functions to handle tasks like AI inference or database management.

Shivay Lamba
Shivay Lamba
8 min
18 Feb, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This talk explores using Rust and WebAssembly to enhance the performance of Node.js applications. By leveraging Rust's efficiency and safety features, and compiling it into WebAssembly bytecode, developers can achieve high-performance computations while ensuring security. WebAssembly enables direct communication with native hardware and is optimized for server-side applications, making it suitable for tasks like AI inference and storage databases. Overall, this talk highlights the potential of combining JavaScript, Rust, and WebAssembly to build performant and portable applications.

1. Introduction to High Performance Node.js

Short description:

Hello, everyone. I'm Shivai Lamba. Today my lightning talk at Node.js Congress 2022 is on high performance Node.js powered by Rust and WebAssembly. The Moore's law states that the number of transistors on a microchip doubles every two years. To overcome hardware limitations, we need to improve software performance while ensuring safety and security. Rust is a safe and modern programming language that offers efficiency and superior performance compared to NodeJS. We can use WebAssembly to compile Rust code into bytecode and manage it with a virtual machine. WebAssembly provides security, lightweight and portable runtime, extreme efficiency, and runtime safety. This talk focuses on using JavaScript, Rust, and WebAssembly to build applications with the performance of Rust and the safety and portability of WebAssembly.

Hello, everyone. I'm Shivai Lamba. I'm currently TensorFlow.js special interest group member and also a working group member from India. And I'm also a mentor for TensorFlow, specifically for TensorFlow.js.

And today my lightning talk at Node.js Congress 2022, it's going to be on high performance Node.js powered by Rust and WebAssembly. Now first, let's briefly talk about the Moore's law. Everyone knows that the Moore's law stated that the number of transistors on a microchip doubles every two years. But if you sort of see from the previous past five years, the size limitation that has caused, has sort of limited the amount of transistors that can be set in a chip. And that has sort of resulted in a plating effect of the performance of a computer hardware. So that means is that in order to be able to overcome this limitation done by the hardware, we have to improve the software with the help of practices of improving software performance. And at the same time, these software improvement performances have to also be safe and secure at the same time.

So for that, we are going to be looking at a few tools that you can use with a lot of different server-side applications and languages such as NodeJS and Rust. Now, why to choose Rust? We have a lot of legacy programming languages like C++, but these are specifically error-prone. But Rust provides a safe, modern and is also equally, if not the same as C++ in terms of efficiency. And also, it is much more capable than NodeJS in terms of performance. You can really crank it up with a lot of highly demanding performance applications that can run very simply with the help of Rust.

And then, the question arises that how can we actually use Rust to code on the web? Well, the older way was to actually compile it to the native libraries and extensions with existing runtimes like NodeJS. But this approach proved to be actually quite unsafe and also required a high amount of maintenance. So, what's the better way? Well, you can use WebAssembly, which is essentially a way to be able to... it provides the instructions to be able to use bytecodes that help interact with the application that you're running it on, specifically the web, with the lower-level applications that you're running on the platform. And we are able to just compile the Rust code into bytecode and then manage it with the help of a virtual machine such as WebAssembly. And what are some of the benefits of WebAssembly virtual machine, which today is also known as Wasmage's runtime? Well, it provides a lot of security out of the box. It is extremely lightweight and portable. It provides extreme efficiency because it uses techniques such as ahead-of-time compilation. It also provides runtime safety as well. And it's specifically optimized for a lot of different server-side applications. And that sort of brings us to the ecosystem that we have for this talk. That we want to use JavaScript for its ease of use to be able to build applications while also have the performance of Rust and have the safety and portability of something like WebAssembly. And that sort of brings us to this architecture that we have. So generally, Node.js runtimes will be based on C++-based runtime.

2. Using WebAssembly for High Performance

Short description:

We can use a WebAssembly-based virtual machine to run Rust-based programs with highly computational tasks, making Node.js applications more efficient. By writing intensive tasks in Rust and using JavaScript functions to call WebAssembly-based functions, we can bundle and compile Rust functions into bytecode hosted on the WebAssembly virtual machine. This not only secures the application but also enables high-performance computations with Rust. WebAssembly can be used for various tasks, including AI inference and storage databases, as it allows direct communication with native hardware and is optimized for server-side applications.

But of course, we have the alternative solution to be able to essentially have a WebAssembly-based virtual machine which can run the Rust-based programs that have the highly computational tasks in them and that can help to make Node.js-based applications highly efficient. And let's see how that actually works. So what we're doing is that we have our Node.js application where all the highly intensive tasks are not written in Node.js but they are actually written in Rust. So with the help of a JavaScript function, we'll be calling the WebAssembly-based functions that help to bundle up the Rust function and compile it into a bytecode that is then served and is actually hosted on the WebAssembly virtual machine, in this case, Wasmx or the SSVM. And that helps to not only secure the application but also all these highly intensive competitions that need to take place are now being done with the help of Rust. And you just have to simply call this function with the help of your Node.js environment. And of course you can use them for a lot of different things, including AI inference, because you're able to directly actually communicate with the native hardware that is your AI hardware or you can use them for things like storage databases as well since WebAssembly is highly optimized for server-side applications.

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

A Guide to React Rendering Behavior
React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
Top Content
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize React rendering.
Speeding Up Your React App With Less JavaScript
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Top Content
Watch video: Speeding Up Your React App With Less JavaScript
Mishko, the creator of Angular and AngularJS, discusses the challenges of website performance and JavaScript hydration. He explains the differences between client-side and server-side rendering and introduces Quik as a solution for efficient component hydration. Mishko demonstrates examples of state management and intercommunication using Quik. He highlights the performance benefits of using Quik with React and emphasizes the importance of reducing JavaScript size for better performance. Finally, he mentions the use of QUIC in both MPA and SPA applications for improved startup performance.
Utilising Rust from Vue with WebAssembly
Vue.js London Live 2021Vue.js London Live 2021
8 min
Utilising Rust from Vue with WebAssembly
Top Content
In this Talk, the speaker demonstrates how to use Rust with WebAssembly in a Vue.js project. They explain that WebAssembly is a binary format that allows for high-performance code and less memory usage in the browser. The speaker shows how to build a Rust example using the WasmPack tool and integrate it into a Vue template. They also demonstrate how to call Rust code from a Vue component and deploy the resulting package to npm for easy sharing and consumption.
React Concurrency, Explained
React Summit 2023React Summit 2023
23 min
React Concurrency, Explained
Top Content
Watch video: React Concurrency, Explained
React 18's concurrent rendering, specifically the useTransition hook, optimizes app performance by allowing non-urgent updates to be processed without freezing the UI. However, there are drawbacks such as longer processing time for non-urgent updates and increased CPU usage. The useTransition hook works similarly to throttling or bouncing, making it useful for addressing performance issues caused by multiple small components. Libraries like React Query may require the use of alternative APIs to handle urgent and non-urgent updates effectively.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.
Making JavaScript on WebAssembly Fast
JSNation Live 2021JSNation Live 2021
29 min
Making JavaScript on WebAssembly Fast
Top Content
WebAssembly enables optimizing JavaScript performance for different environments by deploying the JavaScript engine as a portable WebAssembly module. By making JavaScript on WebAssembly fast, instances can be created for each request, reducing latency and security risks. Initialization and runtime phases can be improved with tools like Wiser and snapshotting, resulting in faster startup times. Optimizing JavaScript performance in WebAssembly can be achieved through techniques like ahead-of-time compilation and inline caching. WebAssembly usage is growing outside the web, offering benefits like isolation and portability. Build sizes and snapshotting in WebAssembly depend on the application, and more information can be found on the Mozilla Hacks website and Bike Reliance site.

Workshops on related topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
Building WebApps That Light Up the Internet with QwikCity
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
Featured WorkshopFree
Miško Hevery
Miško Hevery
Building instant-on web applications at scale have been elusive. Real-world sites need tracking, analytics, and complex user interfaces and interactions. We always start with the best intentions but end up with a less-than-ideal site.
QwikCity is a new meta-framework that allows you to build large-scale applications with constant startup-up performance. We will look at how to build a QwikCity application and what makes it unique. The workshop will show you how to set up a QwikCitp project. How routing works with layout. The demo application will fetch data and present it to the user in an editable form. And finally, how one can use authentication. All of the basic parts for any large-scale applications.
Along the way, we will also look at what makes Qwik unique, and how resumability enables constant startup performance no matter the application complexity.
Next.js 13: Data Fetching Strategies
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Top Content
WorkshopFree
Alice De Mauro
Alice De Mauro
- Introduction- Prerequisites for the workshop- Fetching strategies: fundamentals- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)- Test your build and serve it on Vercel- Future: Server components VS Client components- Workshop easter egg (unrelated to the topic, calling out accessibility)- Wrapping up
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.
React Performance Debugging
React Advanced Conference 2023React Advanced Conference 2023
148 min
React Performance Debugging
Workshop
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)