High Performance Apps with JavaScript and Rust




NodeJS is amazing at lots of things, but computationally intensive or low level tasks aren’t among those things. How can you still leverage the ease of use of NodeJS and do things that are computationally expensive like machine learning, or low level things like computations on a GPU? By using Rust and Node together. Rust is a strongly typed cross platform language that is an excellent choice for handing these exact problems. It’s easy to learn and works very well with Node. This talk will teach you Rust syntax and usage from a JS developers perspective, how to write a Rust library to make it callable from NodeJS and finally actually calling the library from NodeJS. After attending this talk you will:
– Have a basic grasp of writing a Rust library
– Know when and how to use Rust
– Understand how to use the foreign function interface to call out from NodeJS
– Execute callbacks to your Node code from your Rust lib.

EVENT:

NodeJS Interactive 2017

SPEAKER:

Amir Yasin

PERMISSIONS:

The Linux Foundation provided Coding Tech with the permission to republish this video.

CREDITS:

Original video source: https://www.youtube.com/watch?v=Pfbw4YPrwf4

Additional material for JavaScript learners:
https://amzn.to/2JrUINQ Eloquent JavaScript, 3rd Edition: A Modern Introduction to Programming
https://amzn.to/2HwAMYd Simplifying JavaScript: Writing Modern JavaScript with ES5, ES6, and Beyond
https://amzn.to/2Cq5bmL JavaScript: JavaScript Programming.A Step-by-Step Guide for Absolute Beginners
https://amzn.to/2uhIvAP How JavaScript Works
https://amzn.to/2CwS2bU The Modern JavaScript Collection

Original source


13 responses to “High Performance Apps with JavaScript and Rust”

  1. IMO this FFI-Glue code looks more complex than Neon bindings. Maybe Neon improved since this talk was given. At least neon gives you a nice project layout to develop nodejs/rust side-by-side.

    Another option is to compile rust to wasm and use the WASM blob in NodeJS (which is possible with recent NodeJS versions, 11.x ATM).

    So, counting FFI, Neon and WASM, there are three options to integrate Rust into NodeJS – there's no excuse not to try it 😉

  2. a couple clarifications:
    Ownership in rust
    a borrow is like a window into the original variable
    mutable just means changable
    immutable borrows can be passed around freely (so long as the windows die before the original does)
    mutable borrows can only be held by one window, until that window goes out of scope (they block immutable borrows to avoid data races)

    Tuples are basically an easy way to return multiple results in a single package. You access it like you would an array

    Vectors are basically what you would normally think of as an array. The difference happens because one of Rust's focuses is on speed. Vectors live on the heap (big, slow memory) and their length can be changed. Arrays live on the stack (small memory that's close to the cpu) and have be a fixed size known at compile time.

    A closure is basically a mini function that fits inside a variable so it can be handed around. (https://doc.rust-lang.org/book/2018-edition/ch13-01-closures.html#refactoring-with-closures-to-store-code)

    —————
    Rust has some of the best documentation I've seen in a systems language
    Tutorial/reference (clear, effective, very useful): https://doc.rust-lang.org/book/2018-edition/
    An example page from language wiki (clearly explains every part of the language): https://doc.rust-lang.org/std/collections/index.html

  3. tip: when creating a lib project in rust, you can run `cargo run –example foo` where foo lives in the directory `[root]/examples/foo.rs`. This lets you run your application as a program (an example program) but still easy author a lib. This is a better alternative to creating a app if your intent is to create a lib.

Leave a Reply