WebAssembly is a new language and runtime system designed for building high-performance, cross-platform applications. It is based on the same codebase as JavaScript, but uses a different virtual machine and library. What are the benefits of using WebAssembly? There are many benefits to using WebAssembly, including:
- Increased performance due to its optimized codebase and virtual machine.
- Reduced development time due to its ease of use and modularity.
- More accurate and efficient than traditional languages when it comes to memory management and performance.
WebAssembly (Wasm) is low-level machine code that runs in a stack-based virtual machine. This means that it’s much closer to your hardware than JavaScript. That needs to be parsed and fully interpreted before it can run.
Creating Wasm is a bit more involved than writing JavaScript. Although you can hand-code it in Wasm Text Format, this is low level and equivalent to writing assembly code for native execution. More often, you’ll write in an established programming language and compile to Wasm. (More on that later.)
Why Does Wasm Actually Matter?
If the increase in rich web applications is anything to go by, developers, by and large, are pretty comfortable with writing JavaScript. It’s evolved considerably over the past decade with new syntax features and improvements in browser performance. These have elevated it from simple scripts to a language that’s capable of supporting complex, self-contained applications. Why is WebAssembly needed, then?
Wasm doesn’t replace JavaScript. It’s targeted at specific use cases where JavaScript is still a hindrance and will possibly always be. Of these cases, performance is where Wasm particularly excels.
JavaScript-heavy websites have a tendency to feel sluggish and eat up memory. This is especially noticeable on mobile devices, where web apps still lack the “feel” of their native counterparts.
Because Wasm sits so much closer to the hardware, it offers a level of performance that’s barely distinguishable from native apps. This opens the web to a new class of performance-critical applications. If it runs on your desktop, it could run in the browser without any perceptible performance slowdown.
Do We Need More Web Performance?
There will always be an argument that some apps are simply best suited to desktop use. The web provides its own benefits, though, particularly around broad distribution.
Publishing your app on the web gives you the widest possible audience. Anyone can access it from whichever device they own. This increases convenience for your users and means that you have one codebase to maintain.
The real question isn’t whether we need more web performance: Instead, we should be asking whether we need more applications on the web. To answer that question, we can look to functionality-heavy sites that have already adopted Wasm.
AutoCAD is computer-aided design software for architects and engineers. The latest web version debuted at Google I/O in 2018. It uses Wasm to consume components of the existing desktop codebase as is. Some of the code dates back 35 years but now runs in the browser.
Another example is Figma, which already used the asm.js JavaScript subset to optimize its performance. By moving to Wasm, Figma noted a 3x performance improvement. Google uses Wasm too, relying on it to bring Google Earth onto the web for all.
These highly complex applications couldn’t previously exist on the web. If they did, they ran with severe performance penalties compared with their desktop counterparts. Wasm is the architecture necessary to complete the web’s transition from document-oriented pages to a complete application platform. You can even play Doom 3!
Increasing Developer Choice
Beyond performance, WebAssembly matters because it gives developers more flexibility and choice. Excluding the legacy of Flash, Java, and ActiveX plugins, JavaScript has been your only choice when writing code for the browser.
That situation has now been transformed. WebAssembly is a low-level format that other languages compile to. C, C++, C#, Java, Python, Rust, and many more besides are capable of outputting WebAssembly modules. This gives developers many more options when creating code for the web.
You still need JavaScript to load your Wasm modules, but that’s about it. You could implement all of your project’s functionality using your chosen language, and then compile it down to Wasm for the browser.
Developers can now write for the web in the language that best suits them and their application. That might be a strongly typed, object-oriented language, or something focused on functional programming. No longer are you restricted to JavaScript’s loose typing and object prototypes.
Facilitating Code Reuse
WebAssembly brings substantial benefits when it comes to code reuse. You can bring your shared libraries to the web without reimplementing them in JavaScript. This could save hours, days, or weeks of your time.
Using C# as an example, you might have a .NET DLL that contains convenience methods used by your server-side code and your desktop application. Previously, you’d need to implement those methods in JavaScript when it came time to build your web app.
With Wasm, you can recompile your DLL using WebAssembly as the target. The same code becomes accessible in the browser. Wasm reduces waste and duplicate engineering by letting you use what you already have.
Historically, this has only really been possible if your backend used Node.js. You could abstract common logic into a JavaScript library ready for reuse in the frontend. Now, you can work with libraries written in any language.
This works even if you’ll stick with JavaScript for the bulk of your web app. You can interface with Wasm modules through JavaScript, so you can still use your DLL of shared functions. Asynchronous APIs let you exchange data with Wasm so that you can call functions and get their return values.
Summary
WebAssembly is easily dismissed as an obscure technology with little relevance to today’s web apps. Wasm is really targeting the web of tomorrow, where desktop-class apps will run with native performance.
It’s important to look beyond the headline benefits, too. Wasm isn’t just about speed increases. It can also reduce time-to-ship for development teams, especially those that work cross-platform or share logic between the client and the server. Part of the excitement around WebAssembly derives from the web’s transformation into a compilation target, instead of it being a completely standalone platform. Building for the browser no longer means writing JavaScript.
Here, in 2021, WebAssembly adoption remains relatively low. Projects that use it have reported successes—you only need to try Figma or Google Earth to appreciate the Wasm performance standard. You’re likely to see more of Wasm over the next few years as apps, both new and old, are brought into the browser.