## 2 Understanding WebAssembly
### Why WebAssembly Is a Good Fit for Web Platforms  
Wasm is a valuable addition to the web platform, working alongside JavaScript to enhance its capabilities and address certain limitations. The web platform consists of a virtual machine (VM) that executes web app code and a collection of web APIs for controlling browser/device functionality [13]. Traditionally, the VM can only handle JavaScript, which, while is powerful, may face performance challenges in intensive use cases such as 3D games, augmented reality, and image editing. Moreover, downloading and parsing large JavaScript applications can be time-consuming, especially on resource-constrained devices. Wasm serves as a complementary language to JavaScript rather than a replacement. JavaScript excels as a high-level language for web applications, offering flexibility, expressiveness, and an extensive ecosystem of frameworks and libraries. On the other hand, Wasm is a low-level language with a compact binary format that delivers near-native performance. It serves as a compilation target for languages such as C++ and Rust, providing them with a web-compatible execution environment. While JavaScript is dynamically typed and does not require a compilation step, Wasm supports low-level memory models and aims to support garbage-collected languages in the future. With the integration of Wasm into browsers, the VM can now load and execute both JavaScript and Wasm code. These two code types can interact with each other seamlessly. The Wasm JavaScript API allows JavaScript to call exported Wasm code, while Wasm code can import and call JavaScript functions synchronously. Wasm modules, the basic units of Wasm code, exhibit similarities to ES modules in their structure and functionality. Wasm encompasses several key concepts that are essential to understanding its operation within the browser.  
Several of these concepts are directly reflected in the Wasm JavaScript API, as mentioned below:  
* **Module** Represents a Wasm binary that has been compiled by the browser into executable machine code. Similar to a Blob, a Module is stateless and can be shared between windows and workers explicitly through methods such as postMessage(). It declares imports and exports, similar to an ES module.
* **Memory** A resizable ArrayBuffer that holds the linear array of bytes accessed by Wasm's low-level memory instructions for reading and writing.
* **Table**A resizable typed array that stores references, for instance function references, which cannot be stored directly as raw bytes in Memory due to safety and/or portability considerations.
* **Instance** A combination of a Module and its associated runtime state, including a Memory, Table, and imported values. An Instance is akin to an ES module loaded into a specific global context with specific imports.  
The Wasm JavaScript API empowers developers to create modules, memories, tables, and instances. With a Wasm instance, JavaScript code can synchronously invoke its exports, which are exposed as regular JavaScript functions. Conversely, Wasm code can synchronously call arbitrary JavaScript functions by passing them as imports to a Wasm instance. Considering that JavaScript has control over the downloading, compilation, and execution of Wasm code, JavaScript developers can view Wasm as a JavaScript feature designed to generate highly efficient and high-performance functions. In the future, Wasm modules will be loadable in a similar fashion to ES modules, allowing JavaScript to fetch, compile, and import Wasm modules with the same ease as ES modules using the <script type ='module'> syntax.