If you are a tech person, chances are that you’ve already come across the concept of having an assembly-like language for web pages. WebAssembly, or Wasm for short, is a relatively new low-level instruction format that can execute for the Web at near-native speed.
Far from being the first attempt on such goal, since asm.js and others, Wasm has attracted the most hype and gained a lot of traction, being supported by all modern browsers thanks to the joint effort of the World Wide Web Consortium (W3C) and also Mozilla, Microsoft, Google and Apple. You can see it in action not only in a lot of demos, from little games to stunning 3D scenarios, but also in real-world large projects from companies like Autodesk, who leveraged Wasm to release AutoCAD as a web app, and also Figma, who cut their app load time by 3x just by switching to the new standard. Those are some impressive concrete results that showcase the impact of the technology.
WebAssembly was designed to run on top of the browser existing virtual machine, just like JavaScript. The difference lies in the way each language parse and execute their instructions; Wasm is compiled and avoids many of the performance pitfalls that JavaScript suffers from with JIT and its dynamic typing. Instead of scripts, the page downloads a highly optimized binary file, cacheable just like images and stylesheets. Of course, this comes with a complexity you don’t usually have with JS, but that’s why both technologies have its own place in this scenario (more on that later).
So, how does one write WebAssembly? Although it does have a human-readable representation, Wasm is just binary code, so you’ll usually compile other languages to it — and that’s where it gets interesting. There is solid support for C++ and Rust and there are also compilers, albeit with varying degrees of readiness, for a host of other languages like Lua, OCaml, Kotlin, Go, etc and also new languages made specifically for the new standard, like the TypeScript-esque TurboScript. That means that sooner or later you’ll be able to compile to Wasm using your preferred language or the one your team is the most proficient at, for instance.
To get started with WebAssembly, basically you have two paths: if you’re familiar with C++, you can get the Emscripten SDK; the other alternative, if you want to try a more modern language instead, is Rust — in that case, you’ll need rust-wasm. Both toolchains have all the tools you need to run your first piece of WebAssembly and vexcellent tutorials to guide you on that — go ahead and try one!
Naturally, that begs the question: will WebAssembly ever replace JavaScript? Like mentioned before, each technology has its own place and application. JavaScript lies deeply in the foundation of the web tech stack and it’s not going anywhere anytime soon because its simplicity and ubiquity are hard to beat. It is also the bridge between HTML and WebAssembly, so no matter how much you go and port your code, you’ll still need JS to run it and do simple things that are overkill or even impossible with Wasm given the current state of it.
Nonetheless, while we embrace this duality, it will be interesting to see how WebAssembly will grow at the same time that JavaScript continues to evolve and mature. As innovation moves forward and adoption increases, Wasm should bring a new class of applications in the near future to the browser, extending even further the possibilities of the web.
About the author
Pedro Fialho is a Software Engineer at Poatek. He is passionate about code, music and other arts and crafts.