WebAssembly, abbreviated as Wasm, was developed by Microsoft in 2017. As the name suggests, it was introduced to bring assembly like functionality to web. It is a tool that enables web developers to deploy their applications written in any language other than JavaScript on the web. It is a compact binary instruction format for high-level languages such as C, C++, and Rust. Since WebAssembly is a low-level language, it runs at the native speed in the browser.
From the bird's eye view, WebAssembly converts code written in a number of supported languages to Wasm format, which is then converted into machine code and rendered into the browser. The Wasm code is portable and can be rendered on any browser, irrespective of the machine's architecture.
A step-by-step explanation of the working of WebAssembly is given as:
Compilation: The intermediate representation is converted into a .wasm
file using compilers such as Emscripten and Clang. The .wasm
file stores the code in binary format.
Loading: The .wasm
file is loaded into the
Conversion to Machine Code: Once loaded, the code is converted into the machine code using a virtual machine called WebAssembly Runtime. The WebAssembly runtime is integrated into the browser.
Execution: The machine code is easily executable by the browser processor.
The application running in the isolated environment within the browser can call the JavaScript functions and interact with the browser API using JavaScript interoperability.
WebAssembly enhances the functionality of JavaScript and offers the following advantages:
Portable: The .wasm
files are highly portable and lightweight. Since it is a low-level language, it is supported by basic hardware such as the mobile browser.
Debuggable: The .wasm
can be converted to a human-readable text format known as WebAssembly Text Format (WAT) using tools like wasm2wat
. This text file can easily be debugged and modified.
Secure: The .wasm
file is executed in the browser in an isolated environment and communicates through API calls with the outer environment. Like any other web application, it also enforces browser policies.
Backward compatibility: The newer versions of WebAssembly support the older versions of web assembly, avoiding breaking the web.
W3C standard: The WebAssembly is designed according to the W3C standards.
Fast and efficient: The WebAssembly is a low-level language, which gives it the native speed while execution.
Despite the multiple advantages, web assembly has a few limitations. Some of these limitations are:
Web assembly can not directly access DOM. It requires JavaScript or Emscripten.
Old browsers do not support WebAssembly.
Web assembly does not have a garbage collector.
WebAssembly is typically used for the following purposes:
Migration of native desktop applications to web applications.
To perform resource intensive tasks for a web application in a language other than JavaScript. Languages such as R or C++ are more suitable for computation tasks. Hence, WebAssembly enables a regular JavaScript application to call such functions in other languages. It is commonly used in web applications for online gaming, live streaming, video editing, and more.
To deploy web applications written in a wide variety of languages.
It is commonly used in performance-intensive tasks.
WebAssembly is an ideal tool to support web development in multiple languages. Some notable applications that use WebAssembly are Google Earth, Adobe Acrobat and Photoshop, AutoDesk, Figma, AutoCad, and Unity.
Quiz
The code in .wasm
format is converted to which format?
JavaScript
Machine code
Assembly language
Free Resources