# WebAssembly

> The WebAssembly (Wasm) is a binary instruction format that allows running code compiled from other languages in the browser at near-native speed.

## What is WebAssembly and when should I use it?

The low-level binary format executed by the browser at near-native speed. Code written in languages like C++, Rust or Go is compiled to Wasm and used together with JS for heavy tasks such as video editing, games or image processing.

- Topic: Web Application
- Type: technology

## Example

Wrong:

```js
const bytes = await fetch('/module.wasm').then((r) => r.arrayBuffer());
const { instance } = await WebAssembly.instantiate(bytes);
```

Right:

```js
const { instance } = await WebAssembly.instantiateStreaming(
  fetch('/module.wasm'),
);
```

`instantiateStreaming()` compiles while the file downloads, but needs the server to send 'application/wasm'.

## Common mistakes

- Expecting Wasm to speed up DOM-heavy code, while calls across JS and Wasm add overhead.

## Key points

- Modules load with `WebAssembly.instantiateStreaming()` and the server sends 'application/wasm'.
- Heavy Wasm work runs in a worker so it does not block the main thread.
- Wasm is used for CPU-heavy tasks, not for DOM work, which still goes through JS.

## Interview questions

### Is WebAssembly faster than JavaScript?

For CPU-heavy work like image processing, codecs or physics it is often faster and more predictable. For typical app code and DOM updates, JS is just as fast.

### Can WebAssembly access the DOM?

Not directly. It calls JS functions that the page passes in as imports, and those functions update the DOM.

### Which languages compile to WebAssembly?

C, C++, Rust and Go are common, along with others like C# and AssemblyScript.

## Related cards

- [Worker](https://wonderium.dev/js/worker.md)
- [V8](https://wonderium.dev/web/v8.md)
- [Compilation](https://wonderium.dev/engineering/compilation.md)
- [Low-level Language](https://wonderium.dev/engineering/low-level-language.md)

---

- Level: [Web Platform](https://wonderium.dev/web.md)
- HTML version: https://wonderium.dev/web/webassembly
- Last updated: 2026-11-25
