What is WebAssembly (wasm) ?

Bhargav Shah
3 min readFeb 2, 2021

Why, How and What of WebAssembly.

WebAssembly is a new type of code that can be run in modern web browsers — it is a low-level assembly-like language with a compact binary format that runs with near-native performance and provides languages such as C/C++, C# and Rust with a compilation target so that they can run on the web.

WebAssembly is designed to complement and run alongside JavaScript — using the WebAssembly JavaScript APIs, you can load WebAssembly modules into a JavaScript app and share functionality between the two. This allows you to take advantage of WebAssembly’s performance and power and JavaScript’s expressiveness and flexibility in the same apps, even if you don’t know how to write WebAssembly code.

Now, if you have programs in C/C++ or Rust etc. now you can run those programs on the web with help of WebAssembly alongside JavaScript. You can also develop WebAssembly using AssemblyScript (A language made for WebAssembly).

https://www.assemblyscript.org/

Now these has opened doors for running complex programs written in “low-level assembly-like language” on web.

We are already seeing some interesting use cases of WebAssembly, Like TensorFlow has announced — WebAssembly backend for TensorFlow.js (more details here)

TensorFlow WASM backend Performance data
TensorFlow WASM backend Performance data

One more interesting example of WebAssembly could be — A WebAssembly Powered Augmented Reality Sudoku Solver.

In our example using WebAssembly NES emulator and run Super Mario Brothers and Tetris game in our web browser.

TL;DR Use docker image

$ docker pull bhargavshah86/rust-wasm-demo:1.0.0
$ docker run --name rust-wasm-demo -d -p 8080:80 bhargavshah86/rust-wasm-demo:1.0.0
$ # http://localhost:8080/wasm_rust_demo.html

Manual steps:

  1. Download WebAssembly NES emulator (nes_rust_wasm.js and nes_rust_wasm_bg.wasm)
  2. Download ROM files,
  • Super_mario_brothers.nes
  • Tetris.nes

3. HTML and JS code to glue in all parts together

4. Open our “rust_wasm_demo.html” in browser.

Press “Space” to Start, Press “R” to Reset

Conclusion:

WebAssembly brings the performance of native applications to the web in a way that’s completely secure, yet enabling a full functionality expected from games, major applications and the entire spectrum of software you can run on a computer. WebAssembly is just built as a binary format and very compact to download, and also very efficient to compile and execute. There’s a whole bunch of optimizations coming that will drive that even further, allowing huge applications to load up quickly, even on mobile devices.

Thank you for reading 😃

--

--