What is Web Assembly?
WebAssembly (abbreviated as wasm) is a binary instruction format that allows you to run high-performance code in web browsers. WebAssembly is supported by all major web browsers, including Chrome, Firefox, Safari, and Edge. It is designed to compile language ahead of time which allows faster load times.
Although WebAssembly is a low-level assembly language, it does contain a text format that is understandable by humans, making it possible to manually write, inspect, and debug code. It is specified that WebAssembly should run in a secure, sandboxed environment. It will enforce the same-origin and permissions policies of the browser, much like other web scripts.
How Web Assembly runs in web browser?
Compilation Stage: A dedicated compiler like Emscripten is used to compile High level languages like C, C++, Rust source code into WebAssembly code.
Loading Stage: The web app that uses WebAssembly, has a <script> tag with the type attribute set to “module”. The src attribute in <script> tag, points to the location of the .wasm file. The wasm file is downloaded when the browser comes across this script
WebAssembly is a low-level assembly-like language with a compact binary format that runs with near-native performance and provides languages with low-level memory models such as C++ and Rust with a compilation target so that they can run on the web.
|WebAssembly has a high level goal of supporting languages with garbage-collected memory models in the future|
The following are the key factors that could help us understand the differences between JS and WASM:
1. Execution Speed
- Due to the fact that JS is an interpreted language, it could take some time before the browser completely comprehends what it is about to do.
- The strongly typed code used by WASM has already been optimised before it reaches the browser, which speeds up execution significantly.
2. Loading Time
- JS is suitable for smaller tasks.
- WebAssembly is suitable for heavy computation applications. Additionally, it has smaller files, which leads to quicker loading times.
3. Memory Utiltisation
- JS garbage collection automatically handles memory utilisation.
- In WASM, memory usage is fairly complicated. Developers are given linear memory allocations that they must manually control.
4. Garbage Collection
6. Web API Access
- WASM does not currently allow multithreading. However, you can utilise alternative low-level languages that support multithreading.
An interpreted language’s runtime debugging may seem quicker, but it can also let errors and vulnerabilities slide through the gaps.
WebAssembly as a compiled language, debugging occurs before compilation, meaning the code doesn’t compile if errors are found.\