WebAssembly.compileStreaming()

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2021.

* Some parts of this feature may have varying levels of support.

The WebAssembly.compileStreaming() static method compiles a WebAssembly.Module directly from a streamed underlying source. This function is useful if it is necessary to compile a module before it can be instantiated (otherwise, the WebAssembly.instantiateStreaming() function should be used).

Note: Webpages that have strict Content Security Policy (CSP) might block WebAssembly from compiling and executing modules. For more information on allowing WebAssembly compilation and execution, see the script-src CSP.

Syntax

js
WebAssembly.compileStreaming(source)
WebAssembly.compileStreaming(source, compileOptions)

Parameters

source

A Response object or a promise that will fulfill with one, representing the underlying source of a Wasm module you want to stream and compile.

compileOptions Optional

An object containing compilation options. Properties can include:

builtins Optional

An array of strings that enables the usage of JavaScript builtins in the compiled Wasm module. The strings define the builtins you want to enable. Currently the only available value is "js-string", which enables JavaScript string builtins.

importedStringConstants Optional

A string specifying a namespace for imported global string constants. This property needs to be specified if you wish to use imported global string constants in the Wasm module.

Return value

A Promise that resolves to a WebAssembly.Module object representing the compiled module.

Exceptions

  • If source is not a Response or Promise resolving to a Response, the promise rejects with a TypeError.
  • If compilation fails, the promise rejects with a WebAssembly.CompileError.
  • If the source is a Promise that rejects, the promise rejects with the error.
  • If the source's Result has an error (e.g., bad MIME type), the promise rejects with an error.

Examples

Compile streaming

The following example (see our compile-streaming.html demo on GitHub, and view it live also) directly streams a Wasm module from an underlying source then compiles it to a WebAssembly.Module object. Because the compileStreaming() function accepts a promise for a Response object, you can directly pass it a Promise from calling fetch(), without waiting for the promise to fulfill.

js
const importObject = {
  my_namespace: { imported_func: (arg) => console.log(arg) },
};

WebAssembly.compileStreaming(fetch("simple.wasm"))
  .then((module) => WebAssembly.instantiate(module, importObject))
  .then((instance) => instance.exports.exported_func());

The resulting module instance is then instantiated using WebAssembly.instantiate(), and the exported function invoked.

Enabling JavaScript builtins and global string imports

This example enables JavaScript string builtins and imported global string constants when compiling the Wasm module with compileStreaming(), before instantiating it with instantiate() then running the exported main() function (which logs "hello world!" to the console). See it running live.

js
const importObject = {
  // Regular import
  m: {
    log: console.log,
  },
};

const compileOptions = {
  builtins: ["js-string"], // Enable JavaScript string builtins
  importedStringConstants: "string_constants", // Enable imported global string constants
};

WebAssembly.compileStreaming(fetch("log-concat.wasm"), compileOptions)
  .then((module) => WebAssembly.instantiate(module, importObject))
  .then((instance) => instance.exports.main());

Specifications

Specification
WebAssembly Web API
# dom-webassembly-compilestreaming

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
compileStreaming() static method
Chrome – Full support
Chrome 60 (Release date: 2017-07-25)
footnote Full support
Edge – Full support
Edge 16 (Release date: 2017-10-17)
footnote Full support
Firefox – Full support
Firefox 58 (Release date: 2018-01-23)
footnote Full support
Opera – Full support
Opera 47 (Release date: 2017-08-09)
footnote Full support
Safari – Full support
Safari 15 (Release date: 2021-09-20)
footnote Full support
Chrome Android – Full support
Chrome Android 60 (Release date: 2017-08-01)
footnote Full support
Firefox for Android – Full support
Firefox for Android 58 (Release date: 2018-01-22)
footnote Full support
Opera Android – Full support
Opera Android 44 (Release date: 2017-12-11)
footnote Full support
Safari on iOS – Full support
Safari on iOS 15 (Release date: 2021-09-20)
footnote Full support
Samsung Internet – Full support
Samsung Internet 8 (Release date: 2018-07-18)
footnote Full support
WebView Android – Full support
WebView Android 60 (Release date: 2017-08-01)
footnote Full support
WebView on iOS – Full support
WebView on iOS 15 (Release date: 2021-09-20)
footnote Full support
Bun – Full support
Bun 1.2.18 (Release date: 2025-07-03)
footnote Full support
Deno – Full support
Deno 1.12 (Release date: 2021-07-13)
footnote Full support
Node.js – Full support
Node.js 18.1 (Release date: 2022-05-03)
footnote Full support
compileOptions parameter
Chrome – Full support
Chrome 130 (Release date: 2024-10-15)
footnote Full support
Edge – Full support
Edge 130 (Release date: 2024-10-17)
footnote Full support
Firefox – Full support
Firefox 134 (Release date: 2025-01-07)
footnote Full support
Opera – Full support
Opera 115 (Release date: 2024-11-27)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 130 (Release date: 2024-10-15)
footnote Full support
Firefox for Android – Full support
Firefox for Android 134 (Release date: 2025-01-07)
footnote Full support
Opera Android – Full support
Opera Android 86 (Release date: 2024-12-02)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 28 (Release date: 2025-04-02)
footnote Full support
WebView Android – Full support
WebView Android 130 (Release date: 2024-10-15)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support
Bun – No support
Bun
footnote No support
Deno – No support
Deno
footnote No support
Node.js – No support
Node.js
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

See also