WebAssembly.compileStreaming()
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).
The **`WebAssembly.compileStreaming()`** static method compiles a [`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/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()`](/docs/WebAssembly/Reference/JavaScript_interface/instantiateStreaming_static) function should be used).
> [!NOTE]
> Webpages that have strict [Content Security Policy (CSP)](/docs/Web/HTTP/Guides/CSP) might block WebAssembly from compiling and executing modules.
> For more information on allowing WebAssembly compilation and execution, see the [script-src CSP](/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/script-src).
## Syntax
```js-nolint
WebAssembly.compileStreaming(source)
WebAssembly.compileStreaming(source, compileOptions)
```
### Parameters
- `source`
- : A [`Response`](/docs/Web/API/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`
- : An object containing compilation options. Properties can include:
- `builtins`
- : An array of strings that enables the usage of [JavaScript builtins](/docs/WebAssembly/Guides/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`
- : A string specifying a namespace for [imported global string constants](/docs/WebAssembly/Guides/Imported_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`](/docs/WebAssembly/Reference/JavaScript_interface/Module) object representing the compiled module.
### Exceptions
- If `source` is not a [`Response`](/docs/Web/API/Response) or `Promise` resolving to a `Response`, the promise rejects with a TypeError.
- If compilation fails, the promise rejects with a [`WebAssembly.CompileError`](/docs/WebAssembly/Reference/JavaScript_interface/CompileError).
- If the `source` is a `Promise` that rejects, the promise rejects with the error.
- If the response has an incorrect MIME type (instead of `application/wasm`), the promise rejects with a TypeError.
- If consuming the response body fails, the promise rejects with the error.
## Examples
### Compile streaming
The following example (see our [compile-streaming.html](https://github.com/mdn/webassembly-examples/blob/main/js-api-examples/compile-streaming.html) demo on GitHub, and [view it live](https://mdn.github.io/webassembly-examples/js-api-examples/compile-streaming.html) also) directly streams a Wasm module from an underlying source then compiles it to a [`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/Module) object. Because the `compileStreaming()` function accepts a promise for a [`Response`](/docs/Web/API/Response) object, you can directly pass it a `Promise` from calling [`fetch()`](/docs/Web/API/Window/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()`](/docs/WebAssembly/Reference/JavaScript_interface/instantiate_static), 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()`](/docs/WebAssembly/Reference/JavaScript_interface/instantiate_static) then running the exported `main()` function (which logs `"hello world!"` to the console). [See it running live](https://mdn.github.io/webassembly-examples/js-builtin-examples/compile-streaming/).
```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
## Browser compatibility
## See also
- [WebAssembly](/docs/WebAssembly) overview
- [WebAssembly concepts](/docs/WebAssembly/Guides/Concepts)
- [Using the WebAssembly JavaScript API](/docs/WebAssembly/Guides/Using_the_JavaScript_API)
Cite this
"WebAssembly.compileStreaming()". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/JavaScript_interface/compileStreaming_static