This is one of the most frequently asked questions nowadays. So let’s do a quick comparison to see if WebAssembly can replace JS or not.
An Alternative for JS
WebAssembly, generally called wasm, is a compact, fast and portable code that can run on most browsers. WebAssembly is a binary instruction format similar to byte code in java. This code can not be understood by us but by a stack-based virtual machine. A stack-based virtual machine is a virtual machine that considers the operands of all the instructions as they are on a stack.
We generally don’t write code in wasm. Wasm is a compilation target for many programming languages. Many languages compile their code to wasm, making it easier to transfer on the web for deployment purposes.
One more thing, wasm is neither an assembly language nor it’s not built for a specific machine.
On the other hand, WebAssembly is amazingly fast. It is delivered in a small-sized binary format which actually makes it fast to transfer. It is strongly typed.
Working of Wasm
Let us see that how to use wasm and its working.
The binaries are lightweight and faster to textual JS and thus faster to transfer and execute.
We have till now seen that WebAssembly is faster than JS. Let us dig some more deep and see the performance difference between the two.
1. WebAssembly’s binaries are faster to download
JS would need to parse, compile and then optimize the code before executing it which takes longer than the fast binaries WebAssembly has.
It is easy to write code in JS and does not get compiled ahead, since it is a dynamically typed language.
2. WebAssembly manages memory manually
In WebAssembly, there is no garbage pile-up to impact performance.
3. WebAssembly reduces the initial load time
Since wasm is statically typed, it does not need to infer types during the run time. The main reasons that make the initial load time lesser are –
- It has a binary format
- It’s statically typed
- It performs its optimization task in advance while compiling the source code
JS on the other hand does a task to convert the text into a data structure called Abstract Syntax Tree and then convert the Syntax tree to a binary format.
Wasm is threefold better than JS in load time.
Ok, this looks somewhat contradicting to what we have been discussing before. But here we are talking about execution after the optimization of both the codes. Once fully optimized, WebAssembly is slower than JS when executing code in the browser.
Now it’s time for the most controversial question. Will WASM replace JS in near future?
Actually, I don’t think so because JS is still a very convenient choice for many users to perform many tasks. It will be hard to believe that WASM can do all the tasks JS is capable of doing now.
If you want to read more content like this? Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter: https://bit.ly/2Vdlld7.