Rust : The Future of JavaScript?

Rust - Future of JS

Let’s talk about Rust today. Rust is one of those technologies which is emerging, it’s super hot right now, but still, a lot of people don’t exactly know why it is what it is today. We going to spend some time with Rust because it is the technology that will shape the future of web development and it’s an awesome programming language as well. So let’s go ahead and spend some time with Rust in this article.

What is Rust?

Let’s start from the very starting what exactly is Rust? So if you are not living under a rock, you know that Rust is a programming language, which is just like any other programming language, allows you to program computers. It’s not a front-end language which means you cannot use it in front-end web development or things like that. It’s like a back-end development language or a programming language like you have PHP or Java or Python for example. But this has a twist because Rust performs very close to in performance with C and C++, which is the best you get on computers. But at the same time, it’s also a very important pain point of C, C++ developers that is memory management.

So how exactly does Rust do this memory management? We will not go into details of that obviously because that will get complicated and technical, but what you have to realize is Rust uses a unique practice or a unique architecture where Rust tries to enforce a lot of important memory management rules at compile time itself.

So at compile-time, you get the benefits of all the memory management things which you get on higher-level programming languages, but what happens in Rust is that you get that security layer on compilation itself instead of runtime. I mean on runtime, obviously, you have certain checks, but you don’t want a lot of checks on runtime because that decreases your performance and if you want to keep performance as similar to C and C++, you want checks for developer experience on compile-time, but you don’t want a lot of checks of a lot of things on runtime.

So currently if we talk about Rust as a programming language, almost all big players use it in one way or another. AWS uses it, Cloudflare uses it. And a lot of tooling also uses it. For example, Next.JS has started using Rust a little bit.

So why is this the case and why and how does this relate to JavaScript in general?

Problem with JavaScript

Because we know we want to talk a little bit about JavaScript and how does Rust actually relates with JavaScript in this context. Now you see, if we go back a little in the history, you see that JavaScript used to be a technology where we would just start writing a script tag in HTML file and start coding back 10 years back right where we would have JQuery and stuff as the major thing where we will start the project with. Over time we have developed a lot of tooling in JavaScript. Module bundlers like webpack, for example, we have Transpilers like Babel. We have things like Prettier. There’s a bunch of tooling which has been built around JavaScript which helps developers have a better experience holding with them. But the problem is that these tools, also mostly are code in JavaScript itself. Now JavaScript is not a slow language itself, but this definitely suffers a lot of performance when it comes to something like a lot of CPU-bound tasks. If you are trying to run something which is very CPU intensive, then JavaScript would eventually get slower over time compared to its alternatives like Rust, C, and C++.

Javascript

This is because just like I mentioned, one of the main reasons for that is memory management and additional checks at runtime. Because JavaScript is a language that allows you to play safely in a memory area where you know you cannot really corrupt the operating system, memory, and so on. So the runtime has to provide a lot of guarantees and a lot of safety checks. And plus all the weird syntax of JavaScript and supporting all the operations, that require time. For example, if you are assigning a property on an object in JavaScript, it has to go through so many checks before it can do that. Whether you own the object, whether that is a valid value, whether that is something which is even allowed, and so on. But C, C++, and Rust, even when it compiles down to bytecode, you know it’s basically an optimized version of that.

So what happens is that when this tooling is also developed in JavaScript when you are doing so many operations because of things like a Bundler or a Transpiler has to traverse through probably your whole file tree and node modules and everything. So when that happens things eventually get slow right. Webpack might not be slower but when it is used over the top of Next.JS. might be slower. Next.JS used on top of let’s say, a huge project might get even slower. Let’s add a TypeScript layer on top of this which will get even slower. Let’s add a bunch of things over TypeScript like crab GraphQL Codegen in and for example some more type generators. You have for example TS node or something working in the background which is doing this stuff. Then it transpiles into JavaScript and that is checked for linting errors and so on.

When you do a lot of the stuff, what happens is that it slows down your program a lot because JavaScript is not performant for high intensive CPU bound tasks, but still at the end of the day what we get out of all of this work is just a mini JavaScript file which we ship to the browsers or sometimes to servers in cases of serverless environment. So we have optimized a lot on this front which is you know how we can provide the best experience to the users by actually compressing and doing everything and we have done a lot of optimization also in terms of the experience which people get. But in terms of building this optimization pipeline, what we have missed out is on the time developers spend now. I mean, technically speaking, if time was not the concern, you can probably code every single thing in JavaScript and just let it on its own. But that’s a problem because JavaScript is expensive. It’s expensive on the most expensive thing in the universe that is time. It takes some time to execute this whole thing that you have.

Rust is way faster

So that is where technologies like Rust come in and no matter what anyone says to you, the primary reason, the only reason we don’t right stuff in JavaScript and TypeScript and stuff like this is that it’s slower. And of course, you get maybe some memory management things that are positive from Rust, but for the most part, JavaScript can also do that.

The only thing is Rust is way faster, maybe 10 to 100 times faster for some operations when you implement certain things in Rust. Now the situation is in the case of web development we anyway need to get this .JS File out, so this is what ships to the client. This is something that we cannot change that is done and dusted right until we also ship some WASM or something where we can optimize still is this whole pipeline of developer experience that the developer is facing.

So we are trying to put in parts but we are trying to replace parts of JavaScript with the Rust optimized code so that it performs much better in terms of speed on this end the developer end. For example, you’ve heard about SWC which is being worked on by Warcell now, you’ve heard about ESBuild for example, which is not exactly a Rust tool but it’s a go-to tool similar to what SWC is doing. You have heard about Dino as the runtime. Dino also bets aggressively on Rust. So that is also like an important thing. You have heard about Next.JS which also ships with SWC for certain optimization and uses things like Terser which is like mini fire, but still does not work.

These players are now shifting the developer experience on Rust because this makes tooling much faster for developers. We have optimized the JavaScript world we have optimized the easiness of this world, the developer world, now we have to also optimize the time. Because JavaScript can only get so fast. It can only do so much on a single computer. But that is where native programming languages can unlock speed and execution for us.

Of course, Rust also brings interesting things around WASM. For example, if you have something of your own which you want to do on the web itself and it’s you know that it’s computationally expensive. For example, some sort of 3D stuff or some sort of complicated mathematics which involves a lot of CPU, Rust would probably be a much better thing to do. Compile it to WASM and then execute in the browser at near native speeds compared to JavaScript.

But that’s again just like saying how you know this whole thing works. So the point here is that you as the developer, you as a web developer today, don’t and won’t really need Rust to need to learn Rust for working on this layer on the very front, but the moment you go back into building experiences for developers. Today, if you are working on Webpack for example, or today if you are working on SWC or the Terser plugin or whatever plugin, if you know Rust it will be a game-changer for you because you can directly jump into these companies, help them build these projects, help them build these libraries and ship it out, probably 10s of millions of developers in the next few years which will use them as the fundamental underlying technologies to start their projects or start their startups.

Why Rust is important?

So why rust is important as a programming language? because of a lot of web development tech stack, the developer side of things will be built on Rust which will help you code JavaScript better and which will help you ship software faster.

I hope you get something new to learn from this article today. If you do then please tell me in the comment section and share it as much as you can!!

Recommeded Articles :

  1. https://techwithandy.online/top-10-highly-paid-programming-languages/
  2. https://techwithandy.online/facebook-vs-microsoft-metaverse/

Leave a Comment

Your email address will not be published.