In order to utilize Wasm modules in separate threads, the Wasm file must be compiled in the main thread and instantiated in a Web Worker. Let's review this process in more detail:
- A new Web Worker (we'll refer to it as wasmWorker) is created using the Worker() constructor.
- A fetch call is made to retrieve a .wasm file and the arrayBuffer() function is called on the response.
- The resolved value of the arrayBuffer() function is passed to the WebAssembly.compile() function.
- The WebAssembly.compile() function resolves with a WebAssembly.Module instance, which is included in the body of a message posted to the wasmWorker using the postMessage() function.
- Within wasmWorker, the WebAssembly.Module instance from the message ...