Rust Is The Future of JavaScript Infrastructure — Lee Robinson
By Lee Robinson.
Rust is a fast, reliable, memory-saving programming language. Has held the top spot in stackoverflow’s favorite programming language for the past six years (2016,2017,2018,2019,2020,2021). Mozilla created Rust, which is used by Facebook, Apple, Amazon, Microsoft, and Google to develop system infrastructure, encryption, virtualization, and other low-level software.
Why is Rust starting to replace Javascript with important parts of the Web ecosystem that include compression (Terser), compilation (Babel), formatting (Prettier), packaging (webpack), code checking (ESLint), and many more libraries?
What is Rust?
Rust helps developers create fast software that saves memory. It is a modern alternative to C++ or C, which also cares about code safety and compact syntax.
Rust and JavaScript are very different. JavaScript finds variables and objects that are not needed, and then automatically cleans them up. This mechanism is called Garbage Collection. Rust wants developers to plan for manual memory management themselves.
By using Rust, developers have more control over memory and are less painful to use than C++ or Go.
Rust uses a very unique approach to memory management, with a core concept based on “ownership”. Basically, Rust keeps track of “who” is reading and writing to a specific piece of memory. Rust knows who is using a piece of memory and whether it is no longer in use, and can immediately release it. Rust enforces memory rules at compile time, a mechanism that makes memory bugs almost non-existent at runtime. You don’t need to manually track memory, the compiler takes care of it for you. –Discord
Adoption
In addition to the companies that use Rust mentioned above, Rust is also used by many popular open source libraries:
- Firecracker (AWS)
- Bottlerocket (AWS)
- Quiche (Cloudflare)
- Neqo (Mozilla)
Rust has empowered us, and using Rust is one of the best decisions we’ve ever made. Beyond performance, Rust’s engineering philosophy of correctness helps us better manage complexity. We can use Rust’s type system to write complex variables and then have Rust’s compiler do the checking for us. –Dropbox
From JavaScript to Rust
JavaScript is the most widely used programming language, running on every browser device. Over the past decade, a very large ecosystem has been built around JavaScript.
- Webpack: Helps developers pack multiple JavaScript files into one.
- Babel: Helps developers write programs that support older browsers using modern JavaScript syntax.
- Terser: Helps developers compress generated code (to save traffic and confusion).
- Prettier: Helps developers format all their code
- ESLint: Helps developers automatically identify potential problems while writing code.
There are many more libraries like this, with thousands of pieces of code and many more Bug fixes that are the bedrock of today’s Web applications. This tool is written in JavaScript or TypeScript. This mechanism works fine, but we hit JS optimization peaks. The new generation of tools uses more efficient designs that influence previous tools.
SWC
SWC, founded in 2017, aims to provide a scalable rust-based platform dedicated to manufacturing the next generation of rapid development tools. The tool is used by well-known projects such as next.js, Parcel and Deno, as well as companies such as Vercel, ByteDance, Tencent and Shopify.
SWC can be used to compile, compress, package, and use functionality provided by plug-ins. You can use these features to make code changes. Advanced tools mentioned above, such as next.js, also run these transformations.
Deno
Deno, founded in 2018, is a simple, modern, secure JavaScript and TypeScript runtime. Deno uses V8 and was developed using Rust. Deno was created by the founders of Node.js and attempts to replace Node.js. Deno released version 1.0 in May 2020.
Deno’s code censor, code formatter, and document generator are all built using SWC.
esbuild
Esbuild, created in January 2020, is a JavaScript wrapper written in Go that is 10-100 times faster than other existing tools.
I’m trying to build A build tool that A) works well in certain usage scenarios (packaged JavaScript, TypeScript, and maybe CSS) and B) reset the developer community’s expectations for the speed of JavaScript build tools. Our current tools are too slow. –Evan, Esbuild (Source)
Tools for building JavaScript using system programming languages such as Go or Rust were relatively rare when EsBuild was released. Personally, Esbuild has inspired a large number of tool developers to use system programming languages to make these tools faster. Evan chose to use Go:
Written in Rust, it should work fine with some effort. But at a higher level, Go is more comfortable to use. The project was just a side project for me, so it had to be fun. — Evan, EsBuild Author (Source)
Some argue that Rust could have performed better, but both would have served Evan’s purpose and to influence the community:
Even with very basic optimizations, Rust performs better than the best Go optimizations. Writing high performance programs in Rust is much easier than researching very advanced Go. – Discord
Rome
Rome, founded in August 2020, is a code checker, compiler, packer, tester, and much more for JavaScript, TypeScript, HTML, JSON, Markdown, and CSS. Rome’s vision is to replace and unify the chain of tools for front-end development. It was written by Sebastian, who also developed Babel.
Why develop everything?
In order for Babel to serve as an underlying building block for other tools, the changes needed to be made basically completely resize Babel’s architecture. Babel architecture was built in 2014 when I was learning about parsers, syntax trees, and compilers. – Sebastian ( Source )
Rome is written in TypeScript and runs on Node.js, but is currently being rewritten in Rust.
Rust + WebAssembly
WebAssembly (WASM) is a portable low-level language that Rust can compile into. WASM runs in the browser and can interconnect with JavaScript, which is supported by almost all modern browsers.
WASM is much faster than JS, but not as fast as native development. In our tests, Parcel uses WASM 10-20 times slower than native development. – Devon Govett
WASM isn’t the most perfect solution yet, but it can still provide a faster experience for developers. Rust officials have promised higher quality WASM implementations. For developers, this means that you can enjoy the performance of Rust (compared to Go) even if you are still compiling to the Web (using WASM).
Here are some of the early libraries and frameworks:
- Yew
- Percy
- Seed
- Sycamore
- Stork
These Rust-based Web frameworks, compiled into WASM, are not intended to replace JavaScript, but rather to co-exist with JavaScript. This is what we like to see: Rust enables both faster JavaScript functionality and future-oriented compilation to WASM.
Rust all the way.
Why not Rust?
Rust has a very steep learning curve. Rust has a lower level of abstraction than most Web languages.
Rust makes you think about every aspect of your code, which is essential for system development. Rust forces you to think about how memory is shared and copied. Rust forces you to think about rare but realistic edge scenarios to make sure those edge scenarios are also contained. Wherever you can improve your productivity, Rust will help you. — Tom MacWright (Source)
Use of Rust in the Web community is still rare. It hasn’t been used on a large scale yet. For JavaScript tool makers, though, learning Rust is a hurdle. But interestingly, developers prefer to use faster tools, even though it’s harder to contribute code (because Rust is harder). Faster software won out. (The world’s martial arts can only be broken fast)
Right now, it’s still hard to find Rust libraries for services that people like (authentication, databases, transactions, etc.). But I think these problems will take care of themselves when Rust and WASM are used on a large scale. But not now. We need JavaScript tools to help us build Bridges and improve performance over time.
The future of JavaScript tools
I believe Rust is the future of JavaScript tools. Next. Js 12 starts replacing Babel (compiler) and Terser (code compressor) with SWC and Rust in full. Why is that?
- Extensibility: SWC can be used as a Crate inside Next-js, without having to fork the library or workaround design constraints.
- Performance: With SWC, we achieved 3 times refresh speed and 5 times package speed, and there is still a lot of room for performance improvement.
- WebAssembly: Rust supports WASM for all platforms and can take next.js anywhere.
- Community: Rust communities and ecosystems are amazing and continue to grow.
Not only does next.js use SWC, but also:
- Deno’s code reviewers, formatters, and document generators all use SWC.
- Rome is using Rust rewrite and plans to use SWC.
- Dprint, created using SWC, is 30x faster than Prettier.
- Parcel uses SWC to improve 10X performance.
Parcel Using SWC is like using a library. Previously, we used Babel’s parser, written in JS. Now let’s use SWC’s parser, written in Rust. This includes the implementation of scope promotion, dependency collection, and more. This is the same principle that Deno uses to build SWC. – Devon Govett
Although this is an early use of Rust, there are some important points to point out here:
- Plugins: Writing plugins in Rust is still difficult for most JS developers. Using JS to create plug-in system, there will be performance problems. There is no good solution to this problem.
- Packaging: Currently we are interested in swcpack, which is an alternative to WebPack. Although it is still in the development stage, the future can be expected.
- WebAssembly: While it may seem tempting to use Rust and compile into WASM, there is still a lot of work to be done.
Regardless, I am extremely confident that Rust will continue to have a growing impact on the JavaScript ecosystem. Imagine projects around the world that use next.js that already have the performance benefits of Rust when packaged. You can then use next.js just like any other NPM package.
This is the world I want to live in (and exploit).