Creating Node.js libraries with Rust using Neon (Twitch live stream)
In this live stream, we explore the Neon project, a tool that allows you to create Node.js libraries from Rust.
We'll take a look at Neon docs and follow the quickstart tutorial. We'll review the generated code and explore the concept of handle and JS values. Don't worry, we'll make sure to explain everything in simple terms so everyone can follow along!
Then, it's time to get our hands dirty and create a function to sum 2 numbers and a function to sum the square of an array of numbers. We'll even figure out how to downcast a generic JsValue to a JsNumber.
But that's not all! We'll complete and test our implementation while handling casting errors correctly. We'll benchmark against a pure JavaScript implementation, find a bug, and fix it. And if that's not enough, we'll try to parallelize our Rust solution using the Rayon crate.
We're not done yet! We'll test with one million numbers and reduce the data passed to Rust by passing only n as the number of items to sum (and not an entire array). And we'll make sure to explain everything in detail along the way.
So get ready to learn, have fun, and optimize your code like a pro! Let's get started!
Links:
- The Neon project: https://neon-bindings.com/
- The Rayon crate: https://docs.rs/rayon
- The repository with our code samples: https://github.com/lmammino/playing-with-neon
Chapters
00:00:00 Intro
00:00:54 Looking into Neon docs
00:02:35 Following the quickstart tutorial from the docs
00:12:32 Reviewing the generated code and exploring the concept of Handle and JS values
00:17:06 Figuring out how to return JavaScript errors
00:27:14 Creating a function to sum 2 numbers
00:33:24 Creating a function to sum the square of an array of numbers
00:38:01 Figuring out how to downcast a generic JsValue to a JsNumber
00:43:06 Completing and testing our implementation
00:45:33 Handling casting errors correctly
00:51:47 Benchmarking against a pure JavaScript implementation
00:55:57 Finding a bug and fixing it
00:59:00 Trying to parallelize our Rust solution using the Rayon crate
01:04:30 Testing with 1 million numbers
01:05:20 Reducing the data passed to Rust by passing only n as the number of items to sum (and not an entire array)
01:14:11 Closing notes
#rust #nodejs #livecoding
If you like this content make sure to follow us on Twitch too: https://twitch.tv/loige