I tried WebAssembly for the first time and here are my thoughts

WebAssembly

I see more and more articles about why WebAssembly is better than Javascript or why WebAssembly will kill Javascript so I decided to give it a try.

To be clear from the beginning and save you some time, in my opinion WebAssembly will not replace Javascript. Even on the official website it states that: WebAssembly is designed to be a complement to, not replacement of, JavaScript. If you want to find why is that and why one is better than the other please keep reading.

What is WebAssembly?

According to the official documentation from webassembly.org:

“WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.”

So its purpose is to give developers the possibility to create a web app using languages like C, C++ or Rust. Why would anyone want to do that? Here are a couple of reasons:

Why WebAssembly is better than Javascript

  1. Until now, Javascript was the only language you could use on the web for processing data. WebAssembly will allow developers to use many other languages. Think about all the C/C++ or Rust open source modules that could be used aswell.
  2. Some experiments show that parsing WebAssembly code can be more than 20 times faster. This is a big deal especially for mobile devices.
  3. Because WebAssembly is a low-level language with a very compact binary format, is much faster to load than .js files which are textual and can get large very fast.
  4. It will be significantly easier to build cross-platform apps that support web and native environments.
  5. Even though WebAssembly doesn’t currently support multithreading, it will come soon and it will be a big difference in performance compared to Javascript which is running in a single thread.

So these are 5 of the best things about WebAssembly. Now lets see when you should use it instead of Javascript.

When to use WebAssembly?

There are many use cases where you could use it such as AI, VR and augmented reality apps or apps with heavy graphics like games where there are many function calls made in the browser which slows it down. You can find a big list of use cases here.

What I don’t like about WebAssembly

  1. Right now, WebAssembly doesn’t support source maps like Javascript does which makes debugging minified files possible.
  2. Multithreading is currently not supported in WebAssembly.
  3. WebAssembly modules have no access to any platform APIs so if you want to access some platform-specific APIs inside your WebAssembly module, you will have to call it through JavaScript.
  4. There are many ways WebAssembly can be used in malicious applications. One example is for cryptocurrency mining where threat actors will use a victim’s CPU and electricity to make money. The same threat is also in Javascript but since the performance in WebAssembly is better, I think there is a higher chance to happen.
  5. Not enough resources. I wish there was more documentation or places to learn how to use WebAssembly. I guess if everything goes well there will be more and more resources available over the next couple of years.

WebAssembly examples

Ok, enough talking…Lets see some examples:

https://s3.amazonaws.com/mozilla-games/tmp/2017-03-07-ZenGarden-disablewebaudioandwebgl/EpicZenGarden.html

https://s3.amazonaws.com/mozilla-games/tmp/2017-02-21-SunTemple/SunTemple.html

https://s3.amazonaws.com/mozilla-games/tmp/2017-02-21-StylizedRendering/StylizedRendering.html

https://s3.amazonaws.com/mozilla-games/tmp/2017-02-21-PlatformerGame/PlatformerGame.html

Andy

Front-end Engineer & Designer based in Dublin currently working @IBM.
I write about: Javascript, HTML, CSS, React JS, React Native and UI/UX Design.

Add comment