What Is WebAssembly (Wasm)?

What Is WebAssembly (Wasm)?

WebAssembly (Wasm) is transforming web development by enabling high-performance applications in web browsers. Key benefits of Wasm include near-native performance, low overhead, and high portability, making it suitable for diverse applications from gaming and machine learning to IoT and edge computing. This article will explore how Wasm works, its benefits, and its wide range of use cases, from gaming and machine learning in the browser to IoT and edge computing.

WebAssembly, commonly referred to as Wasm, is a binary instruction format designed to enable high-performance applications to run efficiently in web browsers. It functions similarly to Java or .NET bytecode, serving as a compilation target for a wide range of programming languages. This means developers can write code in the languages with which they’re most comfortable—including C/C++, Rust, and Zig—and then compile that code into Wasm, which can be executed at near-native speeds in the browser.

A distinguishing feature of Wasm is its approach to memory management. Wasm supports manual memory management. This contrasts with Java and .NET, which include built-in garbage collectors to automatically handle memory allocation and deallocation. Wasm thereby provides developers with greater control over how memory is used, which can lead to optimized, faster applications. However, this also means that languages that typically rely on garbage collection, such as Python and Ruby, must include their garbage collector in the compiled Wasm module.

Security is another critical aspect of Wasm’s design. Wasm runtimes automatically isolate modules using security mechanisms like control-flow security, ensuring that code runs in a contained environment. This level of isolation is crucial, especially in a browser setting where users might unknowingly run code from various sources. By providing a secure execution environment with minimal overhead, Wasm helps protect users while delivering high-performance web applications.

How Does WebAssembly Work?

Creating and executing a Wasm module requires three steps: compilation, download, and execution.

  1. Compilation: The first step is to compile the source code into Wasm bytecode. This bytecode is in a binary format that requires minimal parsing, enabling Wasm runtimes to execute it efficiently. Depending on the programming language, the generated bytecode may include additional runtime features. For example, Wasm modules compiled from Go are typically larger than those from Rust, because Go requires a garbage collector to be included at compilation time.
  2. Download: The next step is downloading the compiled Wasm modules via HTTP. Web servers provide these modules to clients, such as browsers. Strong isolation of Wasm modules is crucial here because any website can instruct a browser to load a Wasm module. Sandboxing ensures these modules cannot compromise the user’s computer.
  3. Execution: Finally, the downloaded Wasm module is executed in a Wasm runtime. Similar to Java’s JVM, Wasm runtimes (e.g., V8, Wasmer, Wasmtime) can either interpret the bytecode or use just-in-time (JIT) compilation techniques to enhance performance. This approach ensures that Wasm modules can run independently of the underlying hardware and operating system.
Wasm requires three steps: complication, download, and execution
Figure 1: How Wasm works

What Are the Benefits of WebAssembly?

Let’s look at the benefits Wasm brings to the table that can positively impact application development.

Near-Native Performance

Wasm eliminates the dependency on JavaScript, improves startup times, and provides a standardized approach. Benchmarks of computation-intensive algorithms written in C++ show that Wasm delivers performance close to native execution, as the graph below shows.

Wasm exceeds native performance by three measures, slightly underperforms on one measure, and substantially underperforms on one
Figure 2: Native/Wasm performance comparison

Low Module Image Overhead

As the industry as a whole has moved from virtual machines (VMs) to containers, smaller image sizes have become more common. VMs require a complete operating system within their images, while containers can reuse much of the host OS.

Wasm takes this a step further with its standardized interfaces, resulting in even smaller image sizes. For example, a Kubernetes project leveraging Wasm reported the ability to run 50—100 times more pods on a single node compared to traditional containers. Wasm modules are typically just a few megabytes in size, compared to the hundreds of megabytes to multiple gigabytes required by container images.

The image below illustrates the overhead differences between VMs, containers, and Wasm modules.

Wasm offers a lower overhead than VMs or containers
Figure 3: Comparison of VMs, containers, and Wasm overheads

High Portability

Wasm, like other bytecode formats such as Java and .NET, enables the development of portable software. Its standardization ensures that Wasm modules can run consistently across different environments, providing developers with a reliable and versatile tool. Wasm’s portability is partly due to its creation via a collaborative, open-source effort by multiple organizations, all of which have an interest in ensuring Wasm is useable in their own development environments.

Automatic Module Isolation

Wasm automatically sandboxes modules, which provides a level of security that native applications often lack. This isolation ensures that Wasm modules run in a contained environment, preventing untrusted code from compromising the system. Use cases that require executing untrusted code benefit from this feature.

WebAssembly Use Cases

WebAssembly (Wasm) offers a wide range of use cases, driven by its unique features. While many applications are browser-based, Wasm’s versatility extends to numerous non-browser environments as well. Let’s look at the breadth of use cases available with Wasm.

Executing Performance-Intensive Applications in a Browser

Wasm was created to run software in the browser at near-native speed, enabling diverse use cases, from video games to LLMs.

  • Video games: Game engines like Godot and Bevy can be compiled to Wasm, allowing developers to publish 3D games directly on the web. Users can then play these games seamlessly in their browsers. Figure 4 shows a screenshot of a 3D scene created with the Bevy game engine.
  • Machine learning and data processing: Machine learning and data processing often require significant computational power. Wasm allows these tasks to run on the client side, making it possible to use language models like Llama-3 or Mistral-7B directly in the browser.
  • Multimedia processing: Tasks like video decoding benefit from Wasm’s ability to load codecs dynamically. This flexibility ensures that streaming services can distribute the latest and most efficient codecs without relying on the user’s hardware capabilities; see Figure 5 for an FFmpeg encoding example.
  • Augmented reality and virtual reality: AR and VR applications, which require complex 3D rendering, can leverage Wasm for improved performance. Projects like Mozilla Hubs use Wasm to create immersive VR environments that run smoothly in the browser. Figure 5 shows a screenshot of a 3D scene created with the Bevy game engine.
Screenshot of a 3D scene created with the Bevy game engine
Figure 4: Bloom lightning example (Bevy game engine)
Screenshot of a Mozilla Hubs lobby
Figure 5: Mozilla Hubs lobby

Porting Existing Applications to the Web

One of the most popular uses of Wasm is porting existing desktop applications to the web. Tools like LLVM can compile native code into Wasm, making it easier to bring applications online without extensive rewrites.

For example, Wavacity is a Wasm-based version of Audacity, the popular audio editing software, and AutoCAD’s Wasm-based application runs in the browser without installation.

Screenshot of Audacity, a popular audio editing software, running in the browser using Wasm
Figure 6: Wavacity running in the browser

Building Interactive Websites Without JavaScript

Wasm’s multi-language support enables developers to create web applications without relying on JavaScript. Although Wasm modules don’t directly access the browser DOM, they can interact with it through JavaScript glue code. This feature supports frameworks like Blazor for .NET and Dioxus for Rust, allowing developers to use their preferred languages for front-end development.

Enhancing Cryptography and Security

Wasm’s isolation and computation speed are beneficial for cryptography applications. While browsers and JavaScript runtimes support common cryptographic algorithms, Wasm allows for more specialized needs.

Sunscreen is a compiler that uses fully homomorphic encryption (FHE) and zero-knowledge proof (ZKP) techniques with post-quantum cryptography, compiled from Rust to Wasm. It provides cutting-edge cryptographic capabilities directly in the browser.

Executing Applications on Embedded Systems and IoT Devices

While the previous use cases were all browser-based, Wasm also gained popularity beyond the web. Wasm is ideal for embedded systems and IoT devices, which often have limited hardware capabilities. The WebAssembly Micro Runtime (WAMR) is optimized for these low-end devices, leveraging Wasm’s isolation model for enhanced security.

Running Applications at the Edge

Wasm’s features make it well-suited for edge computing, where applications need to run close to users with minimal startup time and high performance. FastEdge, an edge computing service from Gcore, uses Wasm to execute applications quickly and securely, supporting tasks like LLM inference with startup times under a millisecond.

FastEdge is powered by Wasmtime, an open-source WASM runtime created and maintained by the Bytecode Alliance, a joined effort of multiple enterprises invested in Wasm’s future.

The Future of WebAssembly

WebAssembly (Wasm) has proven to be popular and production-ready, with many organizations integrating it into their projects. This success is leading to enhancements in the Wasm standard to address a broader range of use cases.

Three future directions look particularly promising: support for garbage-collected languages, improved inter-module and module-to-host communication, and thread support for parallel computations. Let’s take a closer look at these.

Support for Garbage-Collected Languages

Wasm is currently optimized for languages with manual memory management, such as C/C++, Rust, and Zig. But, as mentioned above, languages that rely on garbage collection, like Python and Ruby, face challenges because they must include their garbage collectors in each Wasm module, leading to increased overhead, especially when multiple modules are used.

To address this, a planned garbage collection (GC) extension will enable these languages to utilize the Wasm runtime’s garbage collector instead. In a browser context, this would mean leveraging the existing JavaScript garbage collector, reducing overhead, and improving efficiency.

Improved Inter-Module and Module-to-Host Communication

Security has always been a top priority for Wasm, with module isolation taking precedence over fast inter-module or module-to-host communication. Currently, communication is managed through message passing or array buffers, which can be slow and cumbersome.

The new component model specification aims to enhance this by facilitating faster and more efficient interactions between modules and hosts, without compromising the robust security features that Wasm is known for.

Thread Support for Parallel Computations

Currently, Wasm supports process-based parallelization, where communication overhead can be significant, particularly for fine-grained workloads. To mitigate this, a proposal for threading support is in the works. This enhancement will allow Wasm to handle parallel computations more effectively, making it suitable for a wider array of high-performance applications that require concurrent processing capabilities.

Summary

WebAssembly (Wasm) is transforming web development with its high-performance, secure, and versatile execution environment, enabling developers to use a variety of programming languages. Its support for manual memory management, automatic module isolation, and anticipated future enhancements make it a powerful tool for both browser and non-browser applications. As Wasm continues to evolve, its adoption across diverse technological landscapes is set to expand, solidifying its role as a pivotal technology in modern software development.

Experience the future of edge computing with FastEdge, powered by WebAssembly (Wasm). Deploy your serverless applications on Gcore’s global edge network with over 180 points of presence. With Wasm runtime on every edge server, FastEdge delivers low-latency performance that keeps your applications fast and efficient. Elevate your app deployment strategy today.

Explore FastEdge

Subscribe to our newsletter

Stay informed about the latest updates, news, and insights.