WebAssembly.Instance() constructor
The WebAssembly.Instance() constructor creates a new Instance object which is a stateful, executable instance of a WebAssembly.Module.
The **`WebAssembly.Instance()`** constructor creates a new
`Instance` object which is a stateful, executable instance of a
[`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/Module).
> [!WARNING]
> Since instantiation for large modules can be expensive,
> developers should only use the `Instance()` constructor when synchronous
> instantiation is absolutely required; the asynchronous
> [`WebAssembly.instantiateStreaming()`](/docs/WebAssembly/Reference/JavaScript_interface/instantiateStreaming_static) method should be used at all other
> times.
## Syntax
```js-nolint
new WebAssembly.Instance(module, importObject)
```
### Parameters
- `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 `module` or
else a [`WebAssembly.LinkError`](/docs/WebAssembly/Reference/JavaScript_interface/LinkError) is thrown.
### Exceptions
- If either of the parameters are not of the correct type or structure, a
TypeError is thrown.
- If the operation fails, one of
[`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) are thrown, depending on the cause of the failure.
- Some browsers may throw a RangeError, as they prohibit compilation and instantiation of Wasm with large buffers on the UI thread.
## Examples
### Synchronously instantiating a WebAssembly module
The `WebAssembly.Instance()` constructor function can be called to
synchronously instantiate a given [`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/Module) object, for example:
```js
const importObject = {
my_namespace: {
imported_func(arg) {
console.log(arg);
},
},
};
fetch("simple.wasm")
.then((response) => response.arrayBuffer())
.then((bytes) => {
const mod = new WebAssembly.Module(bytes);
const instance = new WebAssembly.Instance(mod, importObject);
instance.exports.exported_func();
});
```
However, the preferred way to get an `Instance` is through the asynchronous
[`WebAssembly.instantiateStreaming()`](/docs/WebAssembly/Reference/JavaScript_interface/instantiateStreaming_static) function, for example like this:
```js
const importObject = {
my_namespace: {
imported_func(arg) {
console.log(arg);
},
},
};
WebAssembly.instantiateStreaming(fetch("simple.wasm"), importObject).then(
(obj) => obj.instance.exports.exported_func(),
);
```
## 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.Instance() constructor". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/JavaScript_interface/Instance/Instance