WebAssembly or Wasm is a binary-code format for creating programs that can execute on any computer architecture.
The system comprises a virtual machine and a compiler that translates computer code from programming languages, such as C or C++ into the human-readable .wat and the binary .wasm formats.
WebAssembly’s original goal was to enable programmers develop high-performance applications for web pages by enabling near-native execution speeds inside a browser. But the tech now seems to be evolving beyond browsers and into the cloud.
This guide shows you everything you need to know about this evolving technology, its strengths, and weaknesses, as well as its future potential.
What Is WebAssembly?
WebAssembly is a binary-code format that runs on a virtual machine and that different languages can compile to. It is designed to be portable and can run on a web browser or as a server application.
First released in 2017, WebAssembly is a W3C recommendation since 2019 and is maintained by numerous contributors, including Mozilla, Google, Microsoft, Red Hat, and many more.
How Does WebAssembly Work?
To write code for WebAssembly execution, the developer writes the program in any of the numerous supported languages. Then compile it using a WebAssembly compiler either AOT (Ahead of Time) or JIT (just in time).
How To Create A Sample WebAssembly Application
Creating a simple Wasm application for the web browser is possible with just a few steps as you can see below:
- Download and install the Emscripten SDK.
- Write your sample helloworld.c code or do it in C++/Rust if you prefer.
- Serve the helloworld.html page via your web browser.
The Advantages of WebAssembly
There are many reasons to adopt WebAssembly and its related technologies, they include:
- Near-Native Performance – WebAssembly has such a low-level level design that it offers performances similar to low-level languages such as C and C++.
- Security – The system is designed to be secure by isolating it in a virtual environment with limited system access.
- Open Standard – Many organizations and individuals are contributing to WebAssembly and it is an official W3C recommendation.
- Lightweight – WebAssembly compiles into binary code to be executed in a virtual machine. This code is portable and does not need or come with system files.
- Multi-language Support – Over 50 languages can compile into WebAssembly.
- Platform Independent – The WebAssembly virtual machine is designed for each architecture to present the same runtime environment. All a developer has to do is to write code for WebAssembly.
The Limitations of WebAssembly
- Work in progress – The standard is still in development and hence many features and improvements are yet to come.
- No garbage collection – WebAssembly still lacks garbage collection, although it might soon get implemented.
WebAssembly Vs Java
Java is sort of the earliest implementation of WebAssembly. It was designed as a write-once, run-anywhere language, but developed lots of issues along the way.
Just as with Java, WebAssembly uses a RE (Runtime Environment) to execute its code, but unlike Java, you can write WebAssembly in your language of choice.
WebAssembly Vs Docker Vs Kubernetes
Docker and Kubernetes are two of the popular technologies that power cloud computing. Docker is a container technology that enables developers to package an application with all of its system dependencies into a single package. This makes it easy to deploy the application in any cloud environment in a second or less.
Kubernetes, on the other hand, is an open-source system to manage and deploy containerized applications such as Docker, CRI-O, containerd, and any Kubernetes CRI (Container Runtime Interface) implementation. Kubernetes also makes it easy to scale cloud deployments across multiple servers or virtual machines.
From this standpoint, WebAssembly is more similar to Docker and can be made to run as a containerized cloud application using Kubernetes. It also offers many advantages over Docker, but will probably not replace it.
Beyond The Browser
In the beginning, there were web servers. You had to dedicate a full machine as a server. Then, there were virtual machines, or VMs, which allowed a server to run on just one or more CPU threads. Still, you had to have a dedicated VM.
Cloud computing made it possible to launch and run servers only when needed by containerizing the applications. This led to a reduction in costs, as you only had to pay for what you use – no more dedicated machines. But there are still a few issues.
First, docker files can be several Gigabytes large, and this means a small delay in startup and other management issues. A similar Wasm file, however, can be only a few MB large because it is a pre-compiled binary. This gives it a much faster startup time than Docker, perfect for certain time-critical applications.
Secondly, WebAssembly’s design means the developer does not have to manage and assign system resources, he can simply get to the point using his programming language of choice.
These features make WebAssembly ideal for running cloud micro-services, although Docker applications are still preferable for applications that require more control over the operating environment and filesystem.
For more information, projects like WasmCloud and WasmEdge are developing WebAssembly into a viable could computing platform.
Wasm Use Cases
There are many use cases for WebAssembly, given its peculiar nature. They can be divided into three major groups; in-browser, outside-the-browser, and hybrid setups.
The in-browser type is self-explanatory and outside-the-browser refers mostly to server, IoT, and cloud deployments, while the hybrid refers to a mix of the two.
Following is a look at use cases where Wasm can excel:
- Already existent high-performance code – Old but marvelous code that you can just port to WebAssembly.
- New high-performance code – The ones you have to design and implement from scratch.
- Porting desktop applications to the web – This could open up a new breed of web-based desktop solutions, such as web-based video editing.
- Cloud computing – Running alongside Docker and Kubernetes for new opportunities.
Following is a look at specific applications and scenarios where Wasm can excel:
- Video & image editing
- Game design
- AI applications
- Virtual & augmented reality applications
- Music applications
- P2P applications
- Server-side applications
- Cloud/cluster computations
- Computer-aided design
- OS platform simulation
- Local web server
- Remote desktop applications
- Scientific visualizations
- Hybrid native apps on smartphones
- On IoT devices
List of Supported Languages & Frameworks
Over 50 languages compile to WebAssembly, and here are some of the most popular:
- Blazor – A framework for .NET developers.
- JwebAssembly – Compiler for Java bytecode and related languages like JRuby, Jython, Groovy, Scala, and Kotlin.
- Yew – A Rust framework for creating multi-threaded web apps using WebAssembly.
- AssemblyScript – A language specifically designed to create WebAssembly code.
- Grain – A strongly typed functional language.
You can find more languages here.
List of WebAssembly Runtimes & Tools
- All major browsers have Wasm support
- Pyodide – Python runtime on the browser with the Python scientific stack, including Pandas, NumPy, SciPy.
- wagi – For micro-services and web apps
- Wasi – Modular system interface
- WasmTime – Fast and secure WebAssembly runtime.
- wasmCloud – Cloud environment
- Krustlet – Kubernetes kublete for running Wasm, written in Rust.
- WasmEdge – Cloud and edge runtime environment
We’ve come to the end of our journey into WebAssembly and what it holds for computing. And as you can see, it holds lots of promise.
It is still a work in progress though but in constant development. And if you are interested, you can find more information here, here, and here.