WebAssembly: The Future of High-Performance, Cross-Platform Applications on the Web and Beyond
Have you ever dreamed about a world where you could build software for the web in something other than JavaScript? Imagine writing a program in C++, Rust, or Python and then being able to deliver that software to the end user in a web browser without any installation and in near-native performance. This dream became a reality in December 2019 when WebAssembly became an official W3C standard.
What is WebAssembly (Wasm)?
WebAssembly (Wasm) is a game-changing technology designed to bring near-native performance to web applications. It uses a compact, binary instruction format for a stack-based virtual machine, making it possible to run code efficiently on various platforms, including web browsers, servers, and embedded systems.
Introduced in 2015, Wasm is a compilation target for multiple programming languages, enabling them to run on the web. This innovation has opened new possibilities for applications requiring intensive computation, large data processing, or advanced graphics rendering.
A Brief History
The term “WebAssembly” is inspired by the 1950s assembly language. However, unlike traditional assembly language, which is tied to specific hardware, Wasm is hardware-independent, making it ideal for the web.
Wasm’s journey began in 2015. It was announced on GitHub that it would be released to the public on June 17th, at 9 AM Pacific time. The first major demonstration was running Unity’s Angry Bots in a browser.
By 2017, the minimum viable product (MVP) design was complete, and the preview phase ended. [source]
In late September 2017, Safari 11 was released with WebAssembly support. In February 2018, the WebAssembly Working Group published three public working drafts for the Core Specification, JavaScript Interface, and Web API. [source]
In June 2019, Chrome 75 was released with WebAssembly threads enabled by default. [source]
Over time, Wasm has evolved, with WebAssembly 2.0 currently still in draft since April 2022, featuring advanced instructions for faster and more versatile execution. [source]
Let’s talk about Stack-Based VMs
WebAssembly technology isn’t a virtual machine running inside a browser or other runtimes, as some people think. Wasm is a binary instruction format for a stack-based virtual machine. This statement merely describes the concept that Wasm instructions operate on a value stack instead of register-based machines. This is an abstract, a theoretical model. For some people, virtual machines might imply isolation. In reality, it implies virtualization, for example, taking a physical register-based x86 machine and building a software layer on top of it that pretends to be a stack-based machine that can execute Wasm modules.
Dmitry Soshnikov created a beautiful video explaining the difference between stack and register-based VMs.
So why stack-based ?
Wasm’s stack-based design offers several advantages:
- Portability: A stack-based virtual machine doesn’t rely on a specific hardware architecture, making Wasm versatile across browsers, servers, and embedded systems.
- Simplicity: It eliminates the complexity of managing registers, enabling smaller binary sizes and easier compilation.
- Security: The stack-based model enhances sandboxing and deterministic execution, reducing potential vulnerabilities.
- Performance: While stack-based execution is easier to interpret, modern JIT compilers can optimize it into efficient machine code, rivaling register-based designs.
- Goals and Constraints: WebAssembly’s stack-based design prioritizes portability, security, and compatibility with the web, ensuring smooth interoperability with JavaScript.
- Historical Precedents: Influenced by proven stack-based VMs like JVM and CLR, WebAssembly adopts a trusted and effective model for modern applications.
The whole design rationale of WebAssembly can be found here.
Real-Life Applications
WebAssembly’s capabilities make it a powerful choice for diverse applications:
- Games and Graphics: High-performance games, such as those powered by Unity or Unreal Engine, leverage Wasm for smoother in-browser experiences. As mentioned before, the first major demonstration was Unity’s Angry Bots.
- Scientific Computing: Wasm excels in data-heavy computations, including simulations and machine learning tasks. [example]
- Video and Image Editing: WebAssembly allows complex video and image editing software to run efficiently in the browser. For example CapCut
- Cross-Platform Tools: Applications like Figma and AutoCAD have embraced Wasm to run efficiently in browsers without compromising performance.
Another great example: Figma
Figma’s UI is built with React, while its design tools rely on C++ compiled to Wasm. This hybrid approach highlights how Wasm complements JavaScript rather than replacing it. [source]
Wasm in practice
Wasm code can be written in various languages, compiled into a .wasm binary file, and executed efficiently. For example, a factorial function
written in C:
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
It can be translated to Wasm’s text format (".wat") and then converted into its binary representation with Emscripten. Other tools for many different languages exist. A full list is here.
Here’s a textual representation in ".wat" format for reference:
(func (param i64) (result i64)
local.get 0
i64.eqz
if (result i64)
i64.const 1
else
local.get 0
local.get 0
i64.const 1
i64.sub
call 0
i64.mul
end)
And here is the binary ".wasm" representation:
00 61 73 6D 01 00 00 00
01 06 01 60 01 7E 01 7E
03 02 01 00
0A 17 01
15 00
20 00
50
04 7E
42 01
05
20 00
20 00
42 01
7D
10 00
7E
0B
0B
Beyond Browsers: Embedded Systems
Wasm isn’t limited to web applications. Its lightweight and portable nature makes it a game-changer for IoT and embedded systems, offering a unified platform for running applications across diverse hardware. This could redefine possibilities for edge computing and IoT technology.
A large number of WebAssembly runtimes have been released over the last couple of years. Some prominent ones are:
- Wasmtime
A Rust-based runtime by Bytecode Alliance. It uses the Cranelift compiler, supports languages like C, C++, and Python, and drives new WASI features. It cannot currently target embedded systems. - WASM-Micro-Runtime
A C-based runtime by Bytecode Alliance with full WASI support. It runs on architectures like x86, Arm, XTENSA, and RISC-V, with a small binary size and easy compilation for embedded platforms. Supports AoT compilation. - Wasmer
A versatile runtime by Wasmer with WASI and Emscripten support. It adheres to WebAssembly proposals like SIMD and Threads and includes configurable environments with three compiler backends for optimized performance. - Wasm3
An ultra-lightweight runtime for resource-constrained devices, available as an Arduino module. It interprets wasm modules but lacks AoT/JIT capabilities, making it ideal for deeply embedded or legacy systems. - Wasmi
An interpreter-based runtime by Parity focused on blockchain (e.g., Polkadot). It lacks JIT, AoT, and WASI support and is in maintenance mode, compiling to bare-metal Rust.
This research paper published by IEEE provides sources and more information about WebAssembly's potential in embedded systems.
Challenges and Criticisms
Despite its potential, Wasm has limitations and vulnerabilities:
- DOM Access: WebAssembly cannot directly access the DOM and relies on JavaScript for interaction. [source]
- Memory Constraints: Mobile browsers face challenges allocating large memory blocks, which can limit Wasm’s feasibility for certain applications. [source]
- Security Concerns: Wasm’s ability to obfuscate code has raised concerns about malware and crypto-mining abuse. Studies show a significant portion of Wasm usage in malicious crypto-mining. [1] [2] [3]
However, its sandboxed execution and deterministic nature mitigate some risks, keeping it secure for legitimate applications.
Conclusion
WebAssembly represents a breakthrough in web and cross-platform application performance. By enabling near-native execution speeds in a secure and portable environment, Wasm has become a cornerstone of modern web technologies. From gaming and data analysis to embedded systems and beyond, Wasm is shaping the future of application development.
Whether you’re building for the browser, server, or IoT, Wasm bridges the gap between high performance and platform versatility. Its potential is vast, and its adoption will only grow as the technology matures.