How are Wasm files processed in browser?

Created by Qiwei on 1/12/2024 · Edited on 9/7/2025

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications.

Browser Process Wasm File Steps

WebAssembly (Wasm) files are processed in the browser in the following steps:

Fetch: The Wasm file is fetched from the server. This can be done using the fetch API in JavaScript.

Compile: The fetched Wasm binary is compiled to machine code by the browser. This can be done using the WebAssembly.compile function. The result of this step is a WebAssembly.Module object, which is stateless and can be shared between windows and workers.

Instantiate: The compiled module is instantiated with a set of import values to create a WebAssembly.Instance object. The import values are JavaScript values that represent functions, memory spaces, and global variables that the Wasm code can interact with. This can be done using the WebAssembly.instantiate function.

Call: Once the Wasm instance is created, its exported functions can be called from JavaScript.

Example

For example, we can use wasm-pack build --target web to pack rust code to a JS pacakge.

This command will:

  • Compiles your Rust code to WebAssembly.
  • Runs wasm-bindgen on that WebAssembly, generating a JavaScript file that wraps up that WebAssembly file into a module the browser can understand.
  • Creates a pkg directory and moves that JavaScript file and your WebAssembly code into it.
  • Reads your Cargo.toml and produces an equivalent package.json.
  • Copies your README.md (if you have one) into the package.

The result? You have a package inside the pkg directory.

To use the generated pkg:

// init wasm package
export async function init() {
  const wasmData = require("./pkg/index_bg.wasm");
  await init(wasmData);
  // ...
}

What does init do? If your find its source code in pkg/index.js:

// the `init` function
async function __wbg_init(input) {
    if (wasm !== undefined) return wasm;

    if (typeof input === 'undefined') {
        input = new URL('index_bg.wasm', import.meta.url);
    }
    const imports = __wbg_get_imports();

    if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
        input = fetch(input);
    }

    __wbg_init_memory(imports);

    const { instance, module } = await __wbg_load(await input, imports);

    return __wbg_finalize_init(instance, module);
}

These are steps mentionsed above!

  • It fetches the Wasm file using fetch(input).
  • It compiles and instantiates the Wasm module using __wbg_load(await input, imports).
  • It finalizes the initialization with __wbg_finalize_init(instance, module).

The __wbg_get_imports function creates and returns an object containing the imports required by a WebAssembly module, and the __wbg_init_memory function initializes the memory for a WebAssembly module when the module imports memory from JavaScript.