Definition

WebAssembly

What is WebAssembly (Wasm)?

WebAssembly (Wasm) is a binary instruction format for compiling and executing code in a client-side web browser. Wasm enables far faster performance for complex and demanding web applications compared with common web programming languages such as JavaScript.

Code compiled or translated into Wasm form is a tightly packaged binary that can run on a client computer at near-native speed. This resembles traditional assembly language, which yields some of the fastest software performance available because code is already in a low-level form the computer's processor can run natively.

How does WebAssembly work?

Wasm is the result of compiling traditional code into a specialized Wasm binary file. The Wasm binary file is then imported into the JavaScript engine, where it can be instantiated as an executable.

This process typically involves the following four steps:

  1. Write. Developers can approach writing code for Wasm like any other software project, using established programming languages, software development workflows, and code management and quality standards.
  2. Compile. Wasm is not itself a language but rather the product of compiling and converting code written in each programming language into executable machine code. Many modern software development tools let users compile code to a Wasm binary file, as opposed to a common EXE file. Emscripten, for example, is a popular third-party Wasm compiler.
  3. Import. A Wasm binary file is not executable until loaded into the web browser. When a Wasm binary file is imported into the web browser's JavaScript engine, the browser engine can decode, compile and translate the file into machine code for the client-side computer to execute.
  4. Instance. The JavaScript engine creates an isolated virtual instance -- just as it does for JavaScript files -- where the imported Wasm file can be instantiated and executed.

In a typical scenario, for example, a developer might create a function in C/C++ and compile it into Wasm using a tool such as Emscripten, which generates a WASM binary file and HTML/JS supporting code. From there, the Wasm file is sent to a web client, which uses JavaScript's Wasm API to compile the binary and create a Wasm module that the JavaScript instructions can instantiate. The WebAssembly.instantiateStreaming() function can also load the wasm file and compile and instantiate the Wasm module.

To use Wasm, the browser must support both JavaScript and Wasm. Almost all major web browsers -- including Google Chrome, Microsoft Edge, Firefox, Opera, Safari and Samsung Internet -- currently provide full Wasm support.

Internet Explorer does not support Wasm. To execute Wasm files in Internet Explorer, developers can use tools such as Emscripten to compile the code to asm.js, a highly optimized, low-level subset of JavaScript.

WebAssembly language support

Wasm is not a programming language and is not intended to replace but rather to complement browser-based programming languages such as JavaScript. Developers can write code for Wasm in many different programming languages. This enables developers to write applications in their language of choice and then package the code into Wasm form for the web browser to consume and execute.

JavaScript is already a mature and well-optimized execution environment. Developers can still implement countless basic functions in JavaScript. Whereas JavaScript is designed for running code in local web browsers, however, Wasm provides a compilation and execution environment for high-speed, compact binary code. Thus, JavaScript and Wasm code can coexist and run simultaneously.

Languages such as Rust, C/C++ and Kotlin/Native have direct compilers that translate source code directly into Wasm. Developers working in other languages, such as Java, Lua and .Net, can use third-party tools to convert code into Wasm. The Wasm interpreter can also translate code written in Python and Ruby.

Components of the WebAssembly landscape, including compatible languages, operating systems, hardware, apps, frameworks and extensions.
WebAssembly lets developers work with a wide variety of programming languages, OSes, hardware components, applications and frameworks.

WebAssembly use cases

Wasm is best suited for use cases with the following requirements:

  • High performance. Wasm is useful for web browsers running processor-intensive tasks, such as math calculations, games, graphics applications, and image and video processing.
  • Code uniformity. Wasm eliminates the need to rewrite legacy code into JavaScript functions, a useful feature for web browsers that need to run legacy code while maintaining compatibility across different browsers.
  • Portability. For scenarios in which portability is essential, the same Wasm module can run on any compatible web browser and use the computer's native hardware.

Developers have already applied Wasm in a variety of real-world use cases:

  • Content editing, such as audio, image and video manipulation.
  • High-performance video games that run within web browsers.
  • Scientific simulations, visualizations and other math-intensive tasks.
  • Platform emulation, such as running specialized environments within a browser.
  • Encryption and other security tasks.
  • Remote desktop environments.
  • Developer tools.

Pros and cons of WebAssembly

Wasm offers a variety of benefits to developers, but it also poses some potential drawbacks.

Benefits of WebAssembly

A key advantage of Wasm is its speed and efficiency. Wasm creates machine language code that runs as close to the computer's native hardware as possible, resulting in better software performance and smaller memory footprints. That code is also highly maintainable: Even though compiling to Wasm results in low-level machine language, a text format output is also generated to facilitate debugging Wasm code.

Wasm also offers broad browser and language support. Wasm is almost universally supported by every major modern web browser and is language agnostic. This means developers can write code in their preferred programming language, such as C/C++, Rust or Go, so long as their development tools can compile the code into a Wasm module. Likewise, every browser supports Wasm the same way, so developers can use the same Wasm module in any supported browser without modification.

Drawbacks of WebAssembly

A notable drawback of Wasm is its lack of garbage collection. Because Wasm does not possess native memory management, it relies on the underlying code in the original programming language to provide memory management features.

Ultimately, although JavaScript and Wasm are interoperable, that exchange is not always smooth and seamless. Developers typically limit interactions between JavaScript and Wasm by using JavaScript for some tasks and Wasm for others. For example, JavaScript and Wasm use two different memory models, which can cause inefficient communication. Wasm also lacks access to the document object model (DOM) created when a web browser loads a webpage. This requires developers to use JavaScript or third-party tools to access the DOM.

Finally, because Wasm is built on top of JavaScript and its sandbox environment, Wasm inherits JavaScript's security vulnerabilities. These include limited visibility into memory use, lack of integrity checks and difficulty debugging low-level machine code. Developers should take the same precautions in Wasm security that they would for JavaScript security.

The future of WebAssembly

Wasm is still a relatively new technology, and its standard remains subject to ongoing development through the World Wide Web Consortium (W3C). Consequently, Wasm will continue to evolve as adoption and use cases grow.

In the near term, Wasm's developers are likely to address some of its most notable drawbacks, such as adding support for garbage collection and access to the DOM, to bolster Wasm's performance and stability. W3C committees might also consider streamlining the interoperability between Wasm and JavaScript, enabling better communication between the two.

Wasm use is also expanding from web browsers executing client-side code to server-side program execution, offering alternatives to enterprise and cloud computing containers. In addition, the universal portability offered by low-level Wasm code modules compiled from a standard programming language enables developers to create universal augmentations or plugins for enterprise applications.

This was last updated in June 2023

Continue Reading About WebAssembly

Dig Deeper on DevOps

Software Quality
App Architecture
Cloud Computing
SearchAWS
TheServerSide.com
Data Center
Close