What Is WebAssembly and Why Do You Need It?
WebAssembly (also known as WASM) was launched in 2017 as a binary instruction format for a stack-based virtual machine, developed to run in modern web browsers to provide “efficient execution and compact representation of code on modern processors including in a web browser.”
With WebAssembly you can develop high-performance web applications using open web platform technologies and various languages. WebAssembly makes it possible to create video, audio, graphics, 3D environments, multimedia games, cryptographic computations, and even portable language implementations. On top of that, WebAssembly offers serious performance gains over the standard tools. It is not designed to be used as a language itself, but rather as an effective compilation target for languages like C, C++, and Rust (all of which are very fast by design).
WebAssembly is an open standard, created with the following goals in mind:
- Be executed at near-native speeds.
- Be readable and easily debuggable.
- Be secure.
- Don’t break the web.
But Why WebAssembly?
Traditionally, the web is thought of with two components:
- A virtual machine
- A set of Web APIs
And with more and more cloud native, mobile-first development going on, near-native performance has become crucial for some applications to function in a way that is viable to consumers.
The main benefits of using WebAssembly are:
- Near-native performance.
- Easy debugging.
- Hardware, language, and platform-independent.
There are eight objects that are key to WebAssembly. Those objects are:
- WebAssembly.Module – this object contains stateless WebAssembly code that has been pre-compiled by the browser.
- WebAssembly.Instance – this object is a stateful, executable instance of WebAssembly.Module.
- WebAssembly.Memory – this object is a resizable ArrayBuffer or SharedArrayBuffer that holds raw bytes of memory accessed by a WebAssembly.Instance.
- WebAssembly.CompileError – this object indicates an error during decoding or validation.
- WebAssembly.LinkError – this object indicates an error during a module instantiation.
- WebAssembly.RuntimeError – this object is an error that is thrown when WebAssembly specifies a trap.
What Should You Know Before Diving into WebAssembly?
How Does WebAssembly Work?
In short, there are five steps to creating with WebAssembly:
- Install the Emscripten core (an LLVM/Clang-based compiler that compiles C and C++ source into WebAssembly) and the required toolchain.
- Create your code in C, C++, or Rust.
- Compile your code into WebAssembly with Emscripten.
- Convert the compiled WebAssembly code (via glue code) into an HTML page.
- Serve your new app via HTTP.
WebAssembly Use Cases
There are so many use-cases for WebAssembly. Some of the possibilities include:
- Greenfield/multiplatform development.
- Migrating from desktop-only to desktop and browser-based applications.
- Modernizing older apps written in Silverlight.
- Backward compatibility with older platforms.
- Progressive web apps.
- Mobile apps.
You’ll also find plenty of websites that were built with WebAssembly. Sites like:
You’ll also find any site that runs a Unity game uses WebAssembly. WebAssembly is also perfectly capable of building web-based apps for word processing, spreadsheets, presentations, slides, photo/video editing, and so much more. And although WebAssembly might not be the most widely-used technology at the moment, it’s gaining ground. As of 2020, the vast majority of WebAssembly was being used for crypto mining and gaming. However, given the nature of how web and mobile applications have evolved, I would expect exponentially more and more WebAssembly to be deployed in the near future.