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.
As it turns out, JavaScript is not the best compilation target for other languages.
There were and still are projects like asm.js, a subset of JavaScript that compilers can target.
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.
WebAssembly also interfaces well with JavaScript and vice-versa. JavaScript is to WebAssembly what Python is to C.
You can easily call WASM functions from JavaScript to get performance gains where really necessary.
Traits of WebAssembly
WebAssembly is designed to be:
- Efficient and fast
- Safe
- 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.
-> Safe
The WASM runtime is a memory-safe and sandboxed execution environment.
Especially in the browser, code should not be able to escape. This is one of the core principles of any browser's JavaScript runtime. The code should stay in the browser.
-> 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 WebAssembly is to run in the browser and to integrate into the existing ecosystem.
Easy interfacing between JavaScript and WASM is a core principle.
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 DOM support in the browser. You need to use JavaScript APIs for this
- 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
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
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.
Conclusion
WebAssembly has not fully taken off yet, but it is definitely about to do so. Low-level and library functionality could more and more be implemented in WebAssembly, which opens the web to many more languages than only JavaScript. If this happens, the ecosystem could explode.
Other than the web, WebAssembly slowly takes over the cloud space and might also tackle the blockchain space soon. It will however not affect everyone as transparently because it's a low-level language that is designed as a compile target. The integration between JavaScript and WebAssembly is so good that many developers might not even realize they are using WASM under the hood.
Before You Leave
If you would love to read even more content like this, feel free to visit me on Twitter or LinkedIn.
I'd love to count you as my ever-growing group of awesome friends!