WebAssembly: The Next Language of the Web
At Hashrocket's Winter Miniconf 2020, I presented a summary of my personal one-month audit of WebAssembly. Here's what I learned.
A New Web
Consider, if you will, the web. The web is the largest open technology platform in existence. And what is the language of the web? JavaScript.
Okay, but what is JavaScript? JavaScript is:
- A high-level language
- JIT (just-in-time) compiled
- Object-oriented, with prototypical inheritance
- Curly-bracketed, dynamically typed, has first-class functions, etc.
Is JavaScript fast? It is today. However, JavaScript wasn't always fast. Is JavaScript a good language? That depends on who you ask. Even people who love it (👋) will agree it's far from perfect. Is JavaScript your favorite language? If so, would it be your favorite language if it wasn't so foundational to the web?
Now picture another web. A web where you can write in your favorite languages, such as Elixir, Ruby, Python, and lower-level languages like C/C++, Rust, Go. You'd write code once in your language of choice, and share it across server and client. You even could even write code in the same language that your browser is written in. The code would run on the client with near-native performance.
Some people consider this the Holy Grail of the Web. And if you believe the hype, it is already here, in the form of a language called WebAssembly.
What is WebAssembly?
WebAssembly is a way of taking code written in programming languages other than JavaScript and running that code in the browser. Here's a quote from the official docs:
"WebAssembly, or wasm, is the most significant new technology to come to the web platform in a decade." --Mozilla.org
The 2019 State of JS Survey polled almost 28,000 JavaScript developers, and reported that, of that group, only 7.1% have used WebAssembly. Most respondents know what it is but haven't used it. This tool is new enough that a group of highly engaged front-end devs are just now becoming aware of it.
I think WebAssembly is at least two things:
- A new type of code
- A standard
Let's explore each.
A New Type of Code
WebAssembly is a low-level Assembly-like language with a compact binary format. It provides other languages with a compilation target so they can run on the web. You may be familiar with Assembly: it's a low-level programming language where the instructions closely match the machine code. Some call Assembly "symbolic machine code", so think of this new tool as bringing that machine code into the browser. It's a mashup of technologies.
Most people will use WebAssembly as a compiler target, taking their low-level code and translating it into WebAssembly using a source-to-source compiler, or transpiler. They'll then load their generated WebAssembly module into a JavaScript app, running it side-by-side their JavaScript code at near-native speeds.
So where did this idea come from? It has a precursor called asm.js. Asm.js is a strict subset of JavaScript, and it works like WebAssembly; it's also a compiler target that can run in the browser. Asm.js is supported by C/C++, Rust, Lua, Perl, Python, frameworks, and game engines.
A Standard
WebAssembly is now a W3C recommendation, as of December 2019. It is the fourth language to run natively in browsers, alongside HTML, CSS, and JavaScript. All the major browser teams (Mozilla, Microsoft, Google, and Apple) are participating in the standardization process.
That's why everyone is treating it like such a big deal; this shared ownership suggests WebAssembly might actually cross the chasm of adoption. Furthermore, being a standard means that, like JS, CSS, or HTML, WebAssembly is a foundation upon which we can build. There isn't a Benevolent-Dictator-for-Life or core team determining its direction. Instead, a committee of practitioners, academics, and businesspeople have their hands in the construction process.
How Might WebAssembly Change the Web?
I think this tool is going to affect our workforce as developers, the performance of our apps, and our experience as web consumers.
WebAssembly has the potential to expand the web to developers of more backgrounds and interests. There is a dogma in the web that you must learn JavaScript, and that may become less true. Soon, low-level languages like Go, Rust, and C/C++ will begin to target browsers by directly compiling to WebAssembly. At least 40 languages are already doing this right now. That will change who is able to, and choosing to, write code for the browser.
When it comes to performance, WebAssembly allows for close to the performance of native applications on the web. Even after all of the work people have done to improve JavaScript's performance, WebAssembly has the potential to be orders of magnitude faster than JavaScript.
Our experience as consumers will also change. Imagine gaming in the browser with the performance of a console or desktop; that is coming. Performance gains are likely to translate into more content as online games become bigger, faster, and better.
Yes, WebAssembly lacks some core features such as DOM manipulation and garbage collection that people expect of a modern language. However, most of those missing features can be found in the WebAssembly development roadmap.
Conclusion
I believe that WebAssembly is going to dramatically change the web. I'm excited by the potential it represents. What could you build with it? Let me know on Twitter. In closing, here's a list of resources I collected while learning about WebAssembly:
- A cartoon intro to WebAssembly
- Awesome WebAssembly Languages
- Developer's Guide
- Emscripten & asm.js
- Introduction to WebAssembly
- Made with WebAssembly
- Oh the things you'll compile
- Roadmap
- Rust: Wasm
- Wasm by Example
- WebAssembly Weekly
- WebAssembly as a Platform for Abstraction
- WebAssembly becomes a W3C Recommendation
- WebAssembly format
- WebAssembly hasn't grabbed JavaScript developers
- WebAssembly docs
- WebAssembly: A game changer for the Web | Mozilla
- What are disadvantages of WebAssembly compared to current HTML/JS?
- World of WebAssembly!
- Official site
Thanks for reading.
Photo by Umberto on Unsplash