WebAssembly (Wasm): how it works and why it is necessary
Definition and Purpose: WebAssembly (WASM) is a new, low-level binary format designed to run in modern browsers to achieve better performance on the web. It is fast to load and execute because of its small size.
Compilation: Developers do not write WebAssembly directly; they compile other higher-level languages, such as C, C++, or Rust, to it,,. Tools like Emscripten can be used to compile C/C++ code into a WebAssembly module.
Intermediary Target: WASM acts as an intermediary compiler target for a conceptual machine, allowing the browser to quickly turn the code into any machine’s assembly upon download. This facilitates portability, meaning there is only one compilation step required to run an application in every modern browser.
Relationship with JavaScript: WebAssembly is not a replacement for JavaScript but works alongside it. It is used to offload big chunks of work or performance bottlenecks, while JavaScript typically handles the UI and app logic,. WebAssembly requires JavaScript to interact with Web APIs like the DOM or WebGL.
Key Benefits:
Speed: WASM binaries are smaller, faster to download, decode, and execute than textual JavaScript files. It is statically typed, allowing most optimization to happen during source code compilation, resulting in execution speeds only about 20% slower than native code.
Flexibility: It expands the language options available for web development beyond JavaScript.
Portability: The web is a universal platform, enabling applications to run anywhere with no download or installation required,.
Current Adoption and Examples: WebAssembly support is available in all major browsers, currently supported for 74.93% of global users. Real-world applications include Figma and AutoCAD, which leveraged WASM to improve load times and bring large C++ codebases to the web without a rewrite,.
Future Development: Upcoming features include support for threading and garbage collection, which will make WASM a suitable target for languages like Java, C#, and Go, and the creation of debugging tools.