The grow()
prototype method of the WebAssembly.Memory
object increases the size of the memory instance by a specified number of WebAssembly pages.
The grow()
prototype method of the WebAssembly.Memory
object increases the size of the memory instance by a specified number of WebAssembly pages.
grow(pageIncrease)
pageIncrease
The number of WebAssembly pages you want to grow the memory by (each one is 64KiB in size).
The previous size of the memory, in units of WebAssembly pages.
RangeError
: If the current size added with pageIncrease
exceeds the Memory instance's maximum size capacity.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).
const memory = new WebAssembly.Memory({ initial: 1, maximum: 10 });
We can then grow the instance by one page like so:
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.
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.
const memory = new WebAssembly.Memory({ initial: 1 }); const oldMemoryView = new Uint8Array(memory.buffer); memory.grow(1); // the array is empty! console.log(oldMemoryView); // Uint8Array []
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
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
grow |
57 |
16 |
52 |
No |
44 |
11 |
57 |
57 |
52 |
43 |
11 |
7.0 |
1.0 |
8.0.0 |
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory/grow