WebAssembly.Memory.prototype.grow()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2017.

The grow() prototype method of the WebAssembly.Memory object increases the size of the memory instance by a specified number of WebAssembly pages.

Syntax

js
grow(delta)

Parameters

delta

The number of WebAssembly pages you want to grow the memory by (each one is 64KiB in size).

Return value

The previous size of the memory, in units of WebAssembly pages.

Exceptions

  • RangeError: If the current size added with delta exceeds the Memory instance's maximum size capacity.

Examples

Using grow

The following example creates a new WebAssembly Memory instance with an initial size of 1 page (64KiB), and a maximum size of 10 pages (640KiB).

js
const memory = new WebAssembly.Memory({
  initial: 1,
  maximum: 10,
});

We can then grow the instance by one page like so:

js
const bytesPerPage = 64 * 1024;
console.log(memory.buffer.byteLength / bytesPerPage); // "1"
console.log(memory.grow(1)); // "1"
console.log(memory.buffer.byteLength / bytesPerPage); // "2"

Note the return value of grow() here is the previous number of WebAssembly pages.

Detachment upon growing

Every call to grow will detach any references to the old buffer, even for grow(0)! Detachment means that the ArrayBuffer's byteLength becomes zero, and it no longer has bytes accessible to JavaScript. Accessing the buffer property after calling grow, will yield an ArrayBuffer with the correct length.

js
const memory = new WebAssembly.Memory({
  initial: 1,
});
const oldMemoryView = new Uint8Array(memory.buffer);
memory.grow(1);
// the array is empty!
console.log(oldMemoryView); // Uint8Array []
js
const memory = new WebAssembly.Memory({
  initial: 1,
});
memory.grow(1);
const currentMemoryView = new Uint8Array(memory.buffer);
// the array is full of zeros
console.log(currentMemoryView); // Uint8Array(131072) [ 0, 0, 0, ... ]
// 131072 = 64KiB * 2

For a shared Memory instance, the initial buffer (which would be a SharedArrayBuffer in such case) will not become detached, but rather its length will not be updated. Accesses to the buffer property after growing will yield a larger SharedArrayBuffer which may access a larger span of memory than the buffer from before growing the Memory. Every SharedArrayBuffer from the buffer property will all refer to the start of the same memory address range, and thus manipulate the same data.

Specifications

Specification
WebAssembly JavaScript Interface
# dom-memory-grow

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
grow
Chrome – Full support
Chrome 57 (Release date: 2017-03-09)
footnote Full support
Edge – Full support
Edge 16 (Release date: 2017-10-17)
footnote Full support
Firefox – Full support
Firefox 52 (Release date: 2017-03-07)
footnote Full support
Opera – Full support
Opera 44 (Release date: 2017-03-21)
footnote Full support
Safari – Full support
Safari 11 (Release date: 2017-09-19)
footnote Full support
Chrome Android – Full support
Chrome Android 57 (Release date: 2017-03-16)
footnote Full support
Firefox for Android – Full support
Firefox for Android 52 (Release date: 2017-03-07)
footnote Full support
Opera Android – Full support
Opera Android 43 (Release date: 2017-09-27)
footnote Full support
Safari on iOS – Full support
Safari on iOS 11 (Release date: 2017-09-19)
footnote Full support
Samsung Internet – Full support
Samsung Internet 7 (Release date: 2018-03-16)
footnote Full support
WebView Android – Full support
WebView Android 57 (Release date: 2017-03-16)
footnote Full support
WebView on iOS – Full support
WebView on iOS 11 (Release date: 2017-09-19)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1 (Release date: 2020-05-13)
footnote Full support
Node.js – Full support
Node.js 8 (Release date: 2017-05-30)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also