WebAssembly: Meaning, Advantages, Use Cases & more

Caught up in the WebAssembly hype, but not exactly sure what it is? Here's the low down.

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.

The original goal was to develop near-native execution speeds as you would find with C/C++ applications inside the browser. It is also meant to be a safe, open-standard, debuggable, and version-less web standard that is backward compatible and JavaScript compliant.

How Does WebAssembly Work?

WebAssembly is designed as a runtime engine, such as Java or JavaScript, that can be embedded in a browser or server environment to run computer code compiled in the .wasm binary or .wat text formats.

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).

This code can do most work in the execution environment, but it cannot access or modify a browser’s DOM objects. Any such interaction must pass through JavaScript.

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:

  1. Download and install the Emscripten SDK.
  2. Write your sample helloworld.c code or do it in C++/Rust if you prefer.
  3. Run emcc helloworld.c -s WASM=1 -s helloworld.html in a terminal to compile the code to WebAssembly with Emscripten. It will create the Wasm binary, HTML, and JavaScript files that you need (glue code).
  4. 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.
  • No DOM access – You can’t access a web browser’s DOM directly from WebAssembly, you will need to go through JavaScript.

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 JavaScript

WebAssembly executes in the browser or other host environment, just like JavaScript. However, where JavaScript is delivered to the execution environment in text format and is compiled just in time, WebAssembly has both text and binary formats.

Inside web browsers, JavaScript has access to the DOM, while WebAssembly does not. However, when it comes to execution speed, WebAssembly beats JavaScript, with its near-native code execution speeds.

Speed is where WebAssembly shines and the reason that it is ideal for so many uses. You can load WebAssembly into a browser using JavaScript and the two codes can run together and share resources.

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
  • Encryption
  • 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:

  • C
  • C++
  • R
  • Ruby
  • Swift
  • Go
  • Kotlin
  • Rust
  • PHP
  • 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.
  • Motoko

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.
  • wamr
  • wagi – For micro-services and web apps
  • Wasi – Modular system interface
  • WasmTime – Fast and secure WebAssembly runtime.
  • Wasmer
  • Lucet
  • wasmCloud – Cloud environment
  • Krustlet – Kubernetes kublete for running Wasm, written in Rust.
  • WasmEdge – Cloud and edge runtime environment

Conclusion

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.

Nnamdi Okeke

Nnamdi Okeke

Nnamdi Okeke is a computer enthusiast who loves to read a wide range of books. He has a preference for Linux over Windows/Mac and has been using
Ubuntu since its early days. You can catch him on twitter via bongotrax

Articles: 191

Receive techie stuffs

Tech trends, startup trends, reviews, online income, web tools and marketing once or twice monthly

Leave a Reply

Your email address will not be published. Required fields are marked *