What is WebAssembly?

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.

How WebAssembly works

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.

How WebAssembly works
How WebAssembly works

A step-by-step explanation of the working of WebAssembly is given as:

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

  2. Loading: The .wasm file is loaded into the sandboxedAn isolated environment with controlled access to host environment. environment of the browser by using fetch API.

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

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

Advantages of WebAssembly

WebAssembly enhances the functionality of JavaScript and offers the following advantages:

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

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

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

  4. Backward compatibility: The newer versions of WebAssembly support the older versions of web assembly, avoiding breaking the web.

  5. W3C standard: The WebAssembly is designed according to the W3C standards.

  6. Fast and efficient: The WebAssembly is a low-level language, which gives it the native speed while execution.

Advantages of WebAssembly
Advantages of WebAssembly

Limitations of WebAssembly

Despite the multiple advantages, web assembly has a few limitations. Some of these limitations are:

  1. Web assembly can not directly access DOM. It requires JavaScript or Emscripten.

  2. Old browsers do not support WebAssembly.

  3. Web assembly does not have a garbage collector.

Uses of WebAssembly

WebAssembly is typically used for the following purposes:

  1. Migration of native desktop applications to web applications.

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

  3. To deploy web applications written in a wide variety of languages.

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

1

The code in .wasm format is converted to which format?

A)

JavaScript

B)

Machine code

C)

Assembly language

Question 1 of 30 attempted

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved