The booking page of Mist Homestay, Mrs. Sri's small homestay in the mountains, calculates the price of a stay and edits room photos right in the guest's browser. The calculations can be done by WebAssembly (Wasm): a binary instruction format for a stack-based virtual machine (each term is explained below). A .wasm file holds a sequence of bytes that stand for small instructions such as "take this number" or "multiply the top two numbers on the stack". The browser translates those instructions into machine code: instructions that the CPU (your computer's processor) runs directly. Then it runs them inside a sandbox, a closed space that can't touch the rest of your computer.
Think of Wasm as the cook in the homestay kitchen. The cook works fast from recipes written in a standard code that any kitchen understands. The recipe is the .wasm file, and the kitchen is any browser. But the cook never steps out into the lobby. The one who serves the guests (the page, clicks, the network) is still the receptionist, which is JavaScript. The cook only uses what the receptionist passes through the kitchen window: Wasm only uses the data and functions that JavaScript gives it.
The example for this whole track: Mist Homestay
Mrs. Sri has three rooms: the Terrace Room at $35 a night, the Attic Room at $27.50 and the Garden Room at $42.50. Throughout the WebAssembly track we do two things for her:
- The first function: the price of a stay,
nights × price per night. The Terrace Room for 2 nights = 2 × $35 = $70. The function works on whole numbers, so the code counts in cents: $35 is3500, and 2 × 3500 = 7000 cents, which is $70.00. We write it in Wasm, and later in Rust and Go, two other programming languages. - The heavy work: room photos. Mrs. Sri uploads 1200 × 800 pixel photos of her rooms. In the browser each photo is turned grey (grayscale), made smaller and blurred. We do it in JavaScript first, then in Wasm, and measure honestly which one is faster.
What Wasm is
What the definition's three terms mean:
- Binary: it is bytes, which are numbers from 0 to 255, not text you can read. The price-of-a-stay function, once assembled into a
.wasmfile, is only 47 bytes. - Instruction format: each byte (or a few bytes) is one instruction. The byte
6c(written in hex) meansi32.mul: multiply two 32-bit integers. - Stack-based machine: a virtual machine is a machine made of software, here a part of the browser. Its instructions take numbers from a stack (a pile of numbers) and put their result back on it. There are no registers (places inside a CPU that hold numbers) like on a real CPU.
This is how the price function, stayPrice(2, 3500), runs, one instruction per row:
| Instruction | Meaning | Stack afterwards |
|---|---|---|
local.get 0 |
take the first parameter (nights) | [2] |
local.get 1 |
take the second parameter (price per night) | [2, 3500] |
i32.mul |
take the top two numbers, multiply them | [7000] |
The number left on the stack, 7,000 (cents, so $70.00), is what the function returns. The number types are fixed and few: i32 and i64 (32- and 64-bit integers), f32 and f64 (floating point, numbers with a fraction). Wasm is rarely written by hand. A .wasm file is usually compiled (translated by a program called a compiler) from another language: C and C++ (through Emscripten), Rust, Go or AssemblyScript.
From code to the browser
The order is always the same:
- You write source code.
- A compiler makes a
.wasmfile from that code. - The browser reads the bytes and checks them (validation).
- The browser compiles the bytes to machine code for your CPU, then runs them.
From JavaScript, you load the file and call its function like this:
// 47 bytes made by wat2wasm from price.wat (taken apart in the "Wasm Hello World" lesson)
const bytes = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x60,
0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x0d, 0x01,
0x09, 0x73, 0x74, 0x61, 0x79, 0x50, 0x72, 0x69, 0x63, 0x65, 0x00, 0x00,
0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6c, 0x0b,
]);
WebAssembly.instantiate(bytes).then(({ instance }) => {
const { stayPrice } = instance.exports;
console.log(stayPrice(2, 3500)); // 7000 cents: Terrace Room, 2 nights
});
new Uint8Array([...])is the content of the.wasmfile, written as an array of bytes. Normally you get these bytes withfetch. Here they are written out so the code runs anywhere.WebAssembly.instantiate(bytes)compiles the bytes and creates an instance: a module that is ready to use. A module is the content of one.wasmfile. It returns a Promise: a value that arrives later, when the instance is ready.instance.exportsholds the functions that the module shares with the outside (its exports).stayPriceis called like any JavaScript function.- For a small module like this one there is also a synchronous version, with no Promise:
new WebAssembly.Instance(new WebAssembly.Module(bytes), imports)gives you the sameinstanceright away. In Chrome, this synchronous version is refused on the main thread when the module is larger than 8 MB (RangeError: WebAssembly.Compile is disallowed on the main thread, if the buffer size is larger than 8MB.), so a large module is loaded with the Promise version.
Why can Wasm be faster for some tasks? Two reasons:
- Wasm is already compiled into a binary format close to machine code, so the browser does not have to parse text (read the code as text and find its structure).
- The type of every number is fixed, so the browser does not have to guess types the way it does for JavaScript.
The browser still compiles the Wasm bytes to machine code. V8, the JavaScript engine in Chrome, uses the fast Liftoff compiler first, then the TurboFan compiler for functions called often. But its speed is more predictable, and for heavy computation it comes close to a native program (a program built directly for that CPU). For light work, JavaScript is just as fast. The next lesson measures it.
What Wasm is not
- Not a replacement for JavaScript. JavaScript still handles the page, clicks and the network. Wasm handles heavy computation.
- No direct access to the DOM. The DOM is the content of the page that JavaScript can change through
document. Wasm knows nothing ofdocument,fetchorlocalStorage. It can only call functions that JavaScript hands to it. Such functions are called imports. - Not a plugin like Flash. A plugin was an extra program you had to install in the browser. Wasm is built into the browser and runs in the same sandbox as JavaScript, under the same security rules.
- Not automatically faster. Calling a Wasm function and copying data into the module has a cost.
The module below asks JavaScript for one function to show the total. That function, showTotal, is passed in the import object, the second argument:
// imports.wat contains: (import "display" "showTotal" (func $showTotal (param i32)))
WebAssembly.instantiate(importBytes, {
display: {
showTotal: (total) => {
console.log('Total: $' + (total / 100).toFixed(2)); // JS does the writing
},
},
}).then(({ instance }) => {
const { order } = instance.exports;
order(2, 3500); // Total: $70.00
});
The object's names display and showTotal match the import line in the comment. The order function inside the module calculates 7000 and then calls showTotal. Whatever writes to the console or the page is still JavaScript: a Wasm module can't write "Total: $70.00" onto the page by itself. If you forget to pass the import object, Chrome 147 refuses before any instruction runs, with this error: TypeError: WebAssembly.instantiate(): Imports argument must be present and must be an object.
Where Wasm runs
- Browsers: Chrome 57 and Firefox 52 (March 2017), Safari 11 (September 2017), Edge 16 (October 2017), and every version since.
- Node.js (JavaScript outside the browser): the same
WebAssemblyobject has been there since Node 8. - Servers and the terminal: runtimes (programs that run other programs) such as Wasmtime run
.wasmoutside the browser, with the WASI standard as the door to files and the system clock. - The edge (not the Edge browser): platforms such as Cloudflare Workers run Wasm on servers close to the user.
The same .wasm file runs in all of these places, as long as the imports the module asks for are there. Each engine compiles the file for its own kind of CPU (x86-64, ARM64).
Who uses it
- Figma: its design editor is written in C++ and compiled to Wasm. Load time became more than 3 times faster (Figma blog, June 2017).
- Adobe Photoshop on the web: Photoshop's C++ code was brought to the browser through Emscripten and Wasm (web.dev, 2021).
- Google Sheets: its formula engine is written in Java and compiled to WasmGC, about 2 times faster than the earlier JavaScript version (web.dev, 2024).
The pattern is the same: the app already has a lot of code in another language, or it needs heavy computation at a steady speed.
📌 The standard: the W3C and versions 1.0, 2.0, 3.0
The W3C is the organisation that sets web standards. A finished standard is called a Recommendation.
- 1.0 became a W3C Recommendation on 5 December 2019, and it is the only version with Recommendation status. It has functions, four number types, linear memory and tables.
- 2.0 added sign-extension instructions, non-trapping float-to-int conversions, functions with several results (multi-value), reference types, table instructions and multiple tables, bulk memory operations, and 128-bit vector instructions (SIMD).
- 3.0 was announced on 17 September 2025 as the new "live" standard. It brings extended constant expressions, tail calls, exception handling, multiple memories, a 64-bit address space (memory64), typed references, garbage collection (WasmGC), relaxed vector instructions, a deterministic profile and custom annotations. On the JavaScript side there are JS string builtins.
The W3C document used in this lesson (the 9 October 2026 version) describes release 3.0 and has the status Candidate Recommendation Draft. The Working Group (the W3C group for Wasm) deliberately keeps it at that status as a living standard (a standard that keeps being updated) and does not move it to Recommendation. Which feature runs in which browser is listed at webassembly.org/features.
⚠️ The usual wrong ideas about Wasm
- Assuming Wasm is always faster. A small Wasm function called thousands of times from JavaScript can be slower than plain JavaScript, because every call has a cost. Measure first.
- Assuming Wasm can touch the page. Everything about the DOM,
fetchand storage is done by JavaScript functions, which are given to Wasm as imports. - Forgetting the import object. A module that asks for imports can't be created without that object: you get a
TypeErrorbefore a single instruction runs. - i32 numbers overflow silently.
stayPrice(700000, 3500)should be 2,450,000,000, but an i32 only holds up to 2,147,483,647. It overflows: you get-1844967296, with no error.
TL;DR: WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine. That means: a .wasm file holds bytes (numbers from 0 to 255), the bytes are small instructions, and the instructions work on a stack (a pile of numbers) in a virtual machine (a machine made of software). The browser translates the bytes into machine code (instructions for the CPU) and runs them in a sandbox, a closed and safe space. A .wasm file is usually compiled (translated) from C/C++, Rust or Go. From JavaScript, you load it with WebAssembly.instantiate(bytes) and call the functions it shares (its exports) in instance.exports. Wasm is not a replacement for JavaScript and cannot touch the DOM (the content of the page): it can only call functions that JavaScript gives it as imports. Wasm runs in every modern browser since 2017, and also outside the browser: in Node (JavaScript outside the browser), in the Wasmtime program and at the edge (servers close to the user). Figma, Photoshop on the web and Google Sheets use it. Version 1.0 became a W3C Recommendation (a finished standard) in 2019, 2.0 added SIMD and reference types, and 3.0 (2025) added GC, memory64, exception handling and tail calls. The example for this track: the price of a stay at Mist Homestay (Terrace Room, 2 nights = 7000 cents, $70) and Mrs. Sri's room photos processed in the browser.
⚡ Wasm is a sequence of instruction bytes
Guess first: a .wasm file holds bytes, and those bytes are WebAssembly (Wasm) instructions. Does the browser run a .wasm file as it is?
No. The browser checks the bytes first, then compiles (translates) them to machine code (instructions for your CPU). Only then does it run them, in the same sandbox (a closed space) as JavaScript.
Meaning: WebAssembly is a binary instruction format for a stack-based virtual machine (a machine made of software). It is bytes, and each instruction takes numbers from a stack (a pile of numbers) and puts its result back there. The byte 6c (written in hex) means i32.mul: take the top two numbers on the stack and multiply them. The same .wasm file runs in every modern browser, in Node (JavaScript outside the browser) and at the edge (servers close to the user).
⚡ Load it, then call its exports
Write:
WebAssembly.instantiate(bytes).then(({ instance }) => {
const { stayPrice } = instance.exports;
console.log(stayPrice(2, 3500)); // 7000 cents: Terrace Room, 2 nights
});
Meaning: bytes is the content of the .wasm file. WebAssembly.instantiate(bytes) compiles the bytes and creates an instance, a Wasm module (a .wasm file's content) ready to use. It returns a Promise (a value that arrives later). The functions the module shares (its exports) are in instance.exports, and you call them like any JavaScript function. Here stayPrice(2, 3500) calculates the price of 2 nights in a room at 3500 cents ($35) a night.
⚡ Wasm asks for help through imports
Write:
WebAssembly.instantiate(importBytes, {
display: {
showTotal: (total) => {
console.log('Total: $' + (total / 100).toFixed(2)); // JS does the writing
},
},
});
Guess first: can a Wasm module write "Total: $70.00" onto the page by itself?
No. Wasm knows nothing of document or fetch. This module only calculates 7000 and then calls showTotal, the JavaScript function in the object above. The writing is still done by JavaScript.
Meaning: A JavaScript function handed to a Wasm module is called an import, and the object in the second argument is the import object. That object must have the same shape as the module's line (import "display" "showTotal" ...): showTotal sits inside display. If you forget to pass the import object, the module is refused with a TypeError before a single instruction runs.
⚡ i32 numbers overflow silently
Meaning: i32 is Wasm's type for 32-bit integers, and the stayPrice function multiplies two i32 numbers. stayPrice(700000, 3500) should be 2,450,000,000, but an i32 only holds up to 2,147,483,647. It overflows: you get -1844967296, with no error.