An Introduction To WebAssembly
The Language That Will Change More Than The Web
I have spent a significant part of my dev career in web development, and nothing else excites me more than WebAssembly.
Let me introduce you to the language that will not only change the web but even more.
What Is WebAssembly?
WebAssembly (short: Wasm or WASM) is a binary instruction format for a stack-based virtual machine.
It's a byte-code that can be executed and mainly targets the browser and the web (hence the name).
WASM has two formats:
- A binary format that executed by WebAssembly runtimes
- A human-readable text format for debugging purposes
You can convert one to the other and back again.
WebAssembly was first announced in 2015, and in November 2017, all major browsers had support enabled by default.
It currently exists in version 1.0 while being actively worked on to advance it even further.
Even better: It's standardized!
The goal of WebAssembly
WebAssembly is not a language you should write yourself.
Its goal is to create a compilation target for other languages like C/C++, Rust, Python, etc.
This subset of JS is so limited that ahead-of-time optimization can be applied such that the resulting code runs faster.
But this approach has its limits.
WebAssembly pushes these limits and runs at near-native speed.
Because WebAssembly is basically some flavor of assembly, it's easier to create optimized code for compilers with less effort.
Traits of WebAssembly
WebAssembly is designed to be:
- Efficient and fast
- Open and debuggable
- Part of the open web platform
-> Efficient and fast
WebAssembly is a flavor of assembly. This makes the binary format size- and load-time efficient.
WASM loads and runs fast.
The WASM runtime is a memory-safe and sandboxed execution environment.
-> Open and debuggable
Although the binary format is executed, the textual format is essential to enhance learning, improve debugging, and learn to optimize the resulting code.
A standardized text format and ways to convert between the two helps developers adopting it.
-> Part of the open web platform
The main goal of WebAssebly is to run in the browser and to integrate into the existing ecosystem.
WebAssembly outside of the browser
Although initially designed for the web, WebAssembly has long left the browser and slowly become a near-universal compilation target.
Thanks to WASI, runtimes can also interact with the OS.
Node.js supports WebAssembly for quite some time now, for example.
This allows you to use WASM modules both in the browser and in Node.
And thanks to wasmer, modules can be deployed on any system with a runtime installed.
Current limitations of WebAssembly
WebAssembly is not fully finished yet.
There are still a few things that need to be worked on for WASM to become a sole alternative instead of only a sidecar for other languages.
Some of those limitations are:
- No threading
- No garbage collection
The limitations listed here are definitely being worked on, though, so those problems might soon be solved.
Until then, WebAssembly might not run multi-threaded but can still be used to speed up certain code paths or for programs that don't need threading.
The importance of WebAssembly
"If WASM+WASI existed in 2008, we wouldn't have needed to create Docker. That's how important it is. WebAssembly on the server is the future of computing."
Solomon Hykes, co-founder of Docker
Read this quote slowly and multiple times. Yes, that's a statement.
WASM basically creates a sandboxed execution environment that nearly everyone can finally agree upon.
There were runtimes before, like the JVM, but they never reached the level of acceptance necessary to become universally applicable.
WebAssembly, however, seems to have hit a nail, and its future currently seems bright.
How to get started
If you want to try out WebAssembly, I can give you two recommendations.
There are definitely more ways, but I can't list all here, so I'll limit my recommendations to two options I use the most.
Rust has first-class WebAssembly support. It creates one of the if not the fastest, and most optimized WASM code currently available.
If you don't mind a steep learning curve, Rust might be a great choice for you.
AssemblyScript (AS) is a flavor of TypeScript that compiles to WebAssembly.
Especially if you have worked with TypeScript before, you'll get into AssemblyScript pretty fast.
Before You Leave
I'd love to count you as my ever-growing group of awesome friends!