The WebAssembly.instantiate()
function 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 or
ArrayBuffer
, and performs both compilation and instantiation in one step. The returnedPromise
resolves to both a compiledWebAssembly.Module
and its firstWebAssembly.Instance
. - The secondary overload takes an already-compiled
WebAssembly.Module
and returns aPromise
that resolves to anInstance
of thatModule
. This overload is useful if theModule
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()
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
.