Skip to content
WebAssembly

Linear Memory

Shared, growable byte buffer.

By EZ4Code Team
memorybuffergrow

Code

// Create memory: 1 initial page, 10 max (64KB per page)
const memory = new WebAssembly.Memory({
  initial: 1,
  maximum: 10,
});

// Read/write through a typed array view over the buffer
const view = new Uint32Array(memory.buffer);
view[0] = 42;
view[1] = 100;
console.log(view[0] + view[1]); // 142

// Grow memory (returns the previous page count)
const prev = memory.grow(2);
console.log("grew from", prev, "pages");

// Re-create the view after grow (the old buffer detaches)
const fresh = new Uint32Array(memory.buffer);

Explanation

WebAssembly linear memory is a contiguous, growable ArrayBuffer shared between JS and WASM, organized in 64KB pages. Typed array views such as Uint32Array map directly onto the buffer for fast reads and writes. Growing memory detaches the old buffer, so any existing views must be recreated after a grow.

More WebAssembly Snippets