I know that languages like C and Rust are much more low level than all the other languages i knew, so i just wanted to explore this space a bit more. Apparently, the way of the future is WebAssembly. It’s a framework which allows to run compiled applications on the browser.
As of now, most websites are coded in javascript, an interpreted language. While flexible, this is an obstacle to highly performing applications. This is one of the reasons why the world of gaming never took off within the web framework space. But with the chance of being able to have the same performance as a locally ran C program, the idea of using the web browser as the new portal to high performing applications is looking more plausible by the minute.
So what i wanted to do was to build a simple web app which demonstrated this. What ended up happening was the complete and utter failure of my attempt to do so.after giving the seemingly same logic task to both the contestants — i.e. a rust program and a JavaScript program — across the whole spectrum Javascript always kept outperforming the much advertised “quick” and “close to metal” Rust.
This project also allowed me to explore the conept of JavaScript workers. I just treated them as independent threads. They work concurrently, but on different things, so no race conditions or parallelism problems.
Use the slider to decide the amount of work to be done, press the button, enjoy Rust losing every time
So why did this happen?
Turns out Javascript has A LOT of optimisations going on beneath the surface. In this particular instance i believe the epicentre of the problem was to do with the size of variable allocation. apparently, in javascript, if you have an integer variable, the interpreter allocates the minimum number of bits necessary to complete the task, depending on runtime values; on the contrary, in Rust, this does not happen and has to be done manually. So what i ended up proving was: “if you are not super hardcore about efficiency, don’t even consider Web Assembly”