WebAssembly.instantiate()
The WebAssembly.instantiate() static method allows you to compile and instantiate WebAssembly code. This function has two overloads:
The **`WebAssembly.instantiate()`** static method allows you to
compile and instantiate WebAssembly code. This function has two overloads:
- The primary overload takes the WebAssembly binary code, in the form of a [typed array](/docs/Web/JavaScript/Guide/Typed_arrays) or
ArrayBuffer, and performs both compilation and instantiation in one
step. The returned `Promise` resolves to both a compiled
[`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/Module) and its first [`WebAssembly.Instance`](/docs/WebAssembly/Reference/JavaScript_interface/Instance).
- The secondary overload takes an already-compiled [`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/Module)
and returns a `Promise` that resolves to an `Instance` of that
`Module`. This overload is useful if the `Module` has already
been compiled.
> [!WARNING]
> This method is not the most efficient way of fetching and
> instantiating Wasm modules. If at all possible, you should use the newer
> [`WebAssembly.instantiateStreaming()`](/docs/WebAssembly/Reference/JavaScript_interface/instantiateStreaming_static) method instead, which fetches,
> compiles, and instantiates a module all in one step, directly from the raw bytecode,
> so doesn't require conversion to an ArrayBuffer.
## Syntax
```js-nolint
// Taking Wasm binary code
WebAssembly.instantiate(bufferSource)
WebAssembly.instantiate(bufferSource, importObject)
WebAssembly.instantiate(bufferSource, importObject, compileOptions)
// Taking a module object instance
WebAssembly.instantiate(module)
WebAssembly.instantiate(module, importObject)
WebAssembly.instantiate(module, importObject, compileOptions)
```
### Parameters
- `bufferSource`
- : A [typed array](/docs/Web/JavaScript/Guide/Typed_arrays) or
ArrayBuffer containing the binary code of the Wasm module you want to
compile.
- `module`
- : The [`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/Module) object to be instantiated.
- `importObject`
- : An object containing the values to be imported into the newly-created
`Instance`, such as functions or [`WebAssembly.Memory`](/docs/WebAssembly/Reference/JavaScript_interface/Memory) objects.
There must be one matching property for each declared import of the compiled module or
else a [`WebAssembly.LinkError`](/docs/WebAssembly/Reference/JavaScript_interface/LinkError) is thrown.
- `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
If a `bufferSource` is passed, returns a `Promise` that resolves to a `ResultObject` which contains two
fields:
- `module`: A [`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/Module) object representing the compiled WebAssembly module. This `Module` can be instantiated again, shared via postMessage(), or [cached](/docs/Web/Progressive_web_apps/Guides/Caching).
- `instance`: A [`WebAssembly.Instance`](/docs/WebAssembly/Reference/JavaScript_interface/Instance) object that contains all the [Exported WebAssembly functions](/docs/WebAssembly/Guides/Exported_functions).
If a `module` is passed, returns a `Promise` that resolves to a [`WebAssembly.Instance`](/docs/WebAssembly/Reference/JavaScript_interface/Instance) object.
### Exceptions
- If either of the parameters are not of the correct type or structure,
the promise rejects with a TypeError.
- If the operation fails, the promise rejects with a
[`WebAssembly.CompileError`](/docs/WebAssembly/Reference/JavaScript_interface/CompileError), [`WebAssembly.LinkError`](/docs/WebAssembly/Reference/JavaScript_interface/LinkError), or
[`WebAssembly.RuntimeError`](/docs/WebAssembly/Reference/JavaScript_interface/RuntimeError), depending on the cause of the failure.
## Examples
> [!NOTE]
> You'll probably want to use [`WebAssembly.instantiateStreaming()`](/docs/WebAssembly/Reference/JavaScript_interface/instantiateStreaming_static) in most cases, as it is more efficient than `instantiate()`.
### First overload example
After fetching some WebAssembly bytecode using fetch, we compile and instantiate the
module using the `WebAssembly.instantiate()` function, importing a
JavaScript function into the WebAssembly Module in the process. We then call an [Exported WebAssembly function](/docs/WebAssembly/Guides/Exported_functions)
that is exported by the `Instance`.
```js
const importObject = {
my_namespace: {
imported_func(arg) {
console.log(arg);
},
},
};
fetch("simple.wasm")
.then((response) => response.arrayBuffer())
.then((bytes) => WebAssembly.instantiate(bytes, importObject))
.then((result) => result.instance.exports.exported_func());
```
> [!NOTE]
> You can also find this example at [index.html](https://github.com/mdn/webassembly-examples/blob/main/js-api-examples/index.html)
> on GitHub ([view it live also](https://mdn.github.io/webassembly-examples/js-api-examples/)).
### Second overload example
The following example (see our [index-compile.html](https://github.com/mdn/webassembly-examples/blob/main/js-api-examples/index-compile.html)
demo on GitHub, and [view it live](https://mdn.github.io/webassembly-examples/js-api-examples/index-compile.html) also)
compiles the loaded simple.wasm byte code using the
[`WebAssembly.compileStreaming()`](/docs/WebAssembly/Reference/JavaScript_interface/compileStreaming_static) method and then sends it to a [worker](/docs/Web/API/Web_Workers_API) using
postMessage().
```js
const worker = new Worker("wasm_worker.js");
WebAssembly.compileStreaming(fetch("simple.wasm")).then((mod) =>
worker.postMessage(mod),
);
```
In the worker (see
[`wasm_worker.js`](https://github.com/mdn/webassembly-examples/blob/main/js-api-examples/wasm_worker.js))
we define an import object for the module to use, then set up an event handler to
receive the module from the main thread. When the module is received, we create an
instance from it using the `WebAssembly.instantiate()` method and invoke an
exported function from inside it.
```js
const importObject = {
my_namespace: {
imported_func(arg) {
console.log(arg);
},
},
};
onmessage = (e) => {
console.log("module received from main thread");
const mod = e.data;
WebAssembly.instantiate(mod, importObject).then((instance) => {
instance.exports.exported_func();
});
};
```
### Enabling JavaScript builtins and global string imports
This example enables JavaScript string builtins and imported global string constants when compiling and instantiating the Wasm module with `instantiate()`, before 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/instantiate/).
```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
};
fetch("log-concat.wasm")
.then((response) => response.arrayBuffer())
.then((bytes) => WebAssembly.instantiate(bytes, importObject, compileOptions))
.then((result) => result.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.instantiate()". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/JavaScript_interface/instantiate_static