DEV Community

Cover image for Rust in javascript
Akshay Kannan
Akshay Kannan

Posted on

Rust in javascript

Why do we need rust in javascript ?

We have been using javascript based transpilers and bundlers for a long time now and we have achieved a lot
using this single threaded language. But as we keep building more and more complex stuff, relying a lot on
this language will not be ideal because of various reasons.

One such reason is the javascript's slowness, javascript itself is not slow but, when it comes to CPU intensive tasks
it slows down a lot. Another reason is the garbage collection
in javascript, where memory management is done by the language itself, unlike C++ & Rust. So to speed up the build process
we need a solution in another language, this is where Rust comes in. The thing I love about rust is it's compiler, which
gives self-evident error messages, which languages like javascript fails to do.

Rust

Rust is a low level, memory efficient programming language. It is mainly used in systems,
where performance is important like game engines, operating systems, and databases, etc. So it makes a great choice for
building transpilers and bundlers for javascript. Rust is being used by big companies like Discord, Nextjs, AWS, Cloudfare,
etc. This doesn't mean web developers have to start learning rust right-away, but you can start using tools which are built on
rust and slowly start adopting rust into your flow. The following are the few interesting tools built on top of rust for
javascript,

SWC (Speedy Web Compiler)

SWC is used for bundling and transpiling javascript / typescript and produce a output which is accepted by most browsers.
SWC is said to be 20X faster than Babel, where the former is built on rust and the latter is built using javascript. SWC
is maintained by Vercel and Nextjs is actively using it for their ecosystem. The following are the features they support
right now,

  • Compilation
  • Bundling (swcpack)
  • Minification
  • Transforming with WebAssembly
  • Usage inside webpack (swc-loader)
  • Improving Jest performance (@swc/jest)
  • Custom Plugins

To start using swc you can install @swc/cli and @swc/core, let's say you have javascript file called index.js, then you can
run the following command to produce a transpiled js file in output.js,

npx swc ./index.js -o output.js
Enter fullscreen mode Exit fullscreen mode
// index.js
const test = () => {
  console.log("hello world");
};
Enter fullscreen mode Exit fullscreen mode
// output.js
var test = function () {
  console.log("hello world");
};

//# sourceMappingURL=output.js.map
Enter fullscreen mode Exit fullscreen mode

Check out this swc playground to know more about the functionalities that swc supports.

Deno

Deno is a runtime environment for javascript like Node.js, but unlike
node it can also be used as a runtime environment for typescript and webAssembly. Deno is built on rust and it also
uses SWC for linting and code formatting. It is considered to be the improved version of node in terms of speed and secureness.
It was mainly built to avoid the use of a centralized repository system (npm), and β€œheavy-handed tooling.”

Rust & WebAssembly

Webassembly is a machine language which is accepted by the browsers and they run in near native
speed. It was first introduced in 2017 and now a lot of apps are using it. There are various tools out there to compile
a language's code to Webassembly, so that other programming language developers can contribute to the web. In order to
generate a wasm code you need a compiler to convert it.

There are various libraries / compilers out there to convert different languages to web assembly, for example to convert
C++ to wasm there is Emscripten, also there is Asterius, a Haskell to wasm compiler, and
another library to convert typescript to wasm is Assembly script. Rust actually compiles
to wasm so that we can run it in browsers, The Rust team is committed to a
high-quality and cutting-edge WASM implementation.

Conclusion

Like Rust, we also have another programming language called Go Lang which is slowly finding it's place in javascript tooling
ecosystem. We have esbuild which is said to be the fastest js module bundler and vite is using esbuild to bundle the js
code we write. ESBuild is built using Go. Rust has a steeper learning curve
compared to Go, but Go was not built for building systems or tooling, Go lang is more commonly used for building web apis'
and can be considered as a good alternative for Nodejs.

I love a lot of things about rust starting from it's memory management, direct & self-evident erorr messages, etc, and I
am planning on learning it by building a small js module bundler, about which I might write a blog in the
future πŸ˜‰.

Top comments (0)