buffer.grow(newLength) makes a growable SharedArrayBuffer bigger, in place. You choose this when you create the buffer, by adding a size limit: new SharedArrayBuffer(length, { maxByteLength }). The buffer starts at length bytes and can grow up to maxByteLength bytes later.
The new bytes at the end are filled with zeros, and the old bytes stay exactly as they were. Because it is still the same buffer object, every thread that holds it sees the new size, and typed arrays that follow the whole buffer grow with it. A shared buffer can only grow. It can never get smaller.
Remember
const buf = new SharedArrayBuffer(4, { maxByteLength: 16 });
buf.grow(8) → byteLength 8, the 4 new bytes are 0
buf.grow(8) → fine, same size
buf.grow(4) → RangeError (cannot shrink)
buf.grow(32) → RangeError (over maxByteLength)
new SharedArrayBuffer(4).grow(8) → TypeError (not growable)
Syntax
Create the buffer with a maxByteLength, then call grow() with the new size in bytes.
Make the buffer newLength bytes long. The value must be between the current size and maxByteLength.
new SharedArrayBuffer(length, { maxByteLength })
Create a buffer that grow() is allowed to change.
buffer.growable
Is true for buffers made with maxByteLength, and false for all others.
buffer.maxByteLength
The size limit. For a normal (fixed-length) buffer it is the same as byteLength.
A standard JavaScript method (ES2024) on every SharedArrayBuffer, from SharedArrayBuffer.prototype. It only works on buffers made with the maxByteLength option. Siblings: slice().
Library declarations
JavaScript
// ECMAScript 2024
new SharedArrayBuffer(length, { maxByteLength }) // growable buffer
buffer.grow(newLength) // returns undefined
buffer.growable // true for buffers made with maxByteLength
buffer.maxByteLength // upper limit for grow()
buffer.byteLength // current size, bigger after grow()
Parameters
Parameter
Description
newLength
The new size in bytes. It must be at least the current byteLength and at most maxByteLength. Decimals are cut toward zero (12.9 becomes 12), numeric text such as "10" is converted, and leaving it out counts as 0, which is too small.
Return Value
Return Value
Description
undefined
Nothing is returned. The buffer itself changes, so read buffer.byteLength to see the new size.
RangeError (thrown)
newLength is smaller than the current size, or larger than maxByteLength.
TypeError (thrown)
The buffer is not growable (it was made without maxByteLength), or grow() was called on something that is not a SharedArrayBuffer.
JavaScript Examples
Example 1 — Make a Shared Buffer Bigger
Create a 4-byte growable buffer with two views, fill it, and grow it to 8 bytes. One view follows the buffer, the other has a fixed length.
The Try It page first checks that SharedArrayBuffer and growable buffers (maxByteLength) are available. A normal browser tab is not cross-origin isolated, so there it prints a short explanation and the output above. In Node.js, or on a cross-origin isolated page, it runs the code for real.
How It Works
Opt in when you create it. The option maxByteLength: 16 makes the 4-byte buffer growable, up to 16 bytes. Without it, grow() would throw.
Grow with zeros.grow(8) keeps the old bytes 1,2,3,4 and adds four zero bytes. It returns undefined, so you read the result from byteLength.
Views react differently.tracking was made without a length, so it now shows all 8 bytes. fixed was made with the length 4, so it still shows 4.
Example 2 — Build a Growing Sample Log
Store numbers in a shared buffer whose size is not known in advance. When the buffer is full, the code doubles it, but never beyond maxByteLength.
JavaScript
const buffer = new SharedArrayBuffer(8, { maxByteLength: 32 });
const samples = new Int32Array(buffer);
let count = 0;
function addSample(value) {
if (count === samples.length) {
const bigger = Math.min(buffer.byteLength * 2, buffer.maxByteLength);
buffer.grow(bigger);
}
samples[count] = value;
count++;
}
for (let i = 1; i <= 5; i++) {
addSample(i * 10);
}
console.log("Samples stored: " + count);
console.log("Capacity (items): " + samples.length);
console.log("byteLength: " + buffer.byteLength);
console.log("Values: " + samples.slice(0, count).join(","));
The Try It page first checks that SharedArrayBuffer and growable buffers (maxByteLength) are available. A normal browser tab is not cross-origin isolated, so there it prints a short explanation and the output above. In Node.js, or on a cross-origin isolated page, it runs the code for real.
How It Works
Count what you use. The buffer starts with 8 bytes, which is room for 2 numbers, because each Int32Array item takes 4 bytes. count tracks how many are filled.
Grow only when full. The third sample needs more room, so the buffer doubles to 16 bytes. The fifth sample doubles it again to 32 bytes, which is exactly maxByteLength.
No copying.samples follows the buffer, so samples.length becomes 8 without creating a new array, and slice(0, count) shows only the stored values.
Example 3 — Check the Rules and Catch the Errors
Try growing a fixed buffer, shrinking, going over the limit, and two sizes that are allowed. The catch block prints the error type only, because the message text differs between engines.
growable: true
fixed growable: false
maxByteLength: 12
fixed buffer -> TypeError
shrink to 4 -> RangeError
over the limit -> RangeError
same size -> ok, byteLength 8
grow to 12 -> ok, byteLength 12
The Try It page first checks that SharedArrayBuffer and growable buffers (maxByteLength) are available. A normal browser tab is not cross-origin isolated, so there it prints a short explanation and the output above. In Node.js, or on a cross-origin isolated page, it runs the code for real.
How It Works
Check growable first. A buffer made without maxByteLength reports growable as false, and its maxByteLength is just its normal size.
Two kinds of errors. Growing the fixed buffer throws a TypeError. Asking for 4 bytes (smaller) or 64 bytes (over the limit of 12) throws a RangeError.
Allowed sizes work. Growing to the same size changes nothing but is allowed. Growing to exactly maxByteLength (12) also works, and a failed call never changes byteLength.
Compatibility
Browser & Runtime Support
SharedArrayBuffer.prototype.grow() is part of ES2024, together with growable shared buffers. Browsers only provide SharedArrayBuffer on cross-origin isolated pages.
✓ Newer · ES2024
sharedBuffer.grow()
Supported in Chrome 111+, Edge 111+, Firefox 128+, Safari 16.4+, Opera 97+ and Node.js 20+. Internet Explorer never got it.
ES2024Standard since
Google Chrome111+ · Desktop & Mobile
Full support
Microsoft Edge111+ · Chromium based
Full support
Mozilla Firefox128+ · Desktop & Mobile
Full support
Apple Safari16.4+ · macOS & iOS
Full support
Opera97+ · Chromium based
Full support
Samsung Internet22+ · Android
Supported
Android WebView111+ · Chromium based
Supported
Node.js20+ · Server runtime
Supported
DenoRecent versions · JavaScript runtime
Supported
BunRecent versions · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
sharedBuffer.grow()Recent browsers
Bottom line: Check support with typeof SharedArrayBuffer.prototype.grow === "function". If it is missing, allocate a bigger SharedArrayBuffer and copy the bytes with new Uint8Array(bigger).set(new Uint8Array(old)). In browsers the page must also be cross-origin isolated.
Frequently Asked Questions
It makes a growable SharedArrayBuffer bigger without creating a new buffer. The old bytes stay, the new bytes are zeros, and every thread that holds the buffer sees the new size. It returns undefined.
Pass the maxByteLength option to the constructor: new SharedArrayBuffer(8, { maxByteLength: 64 }). The buffer starts at 8 bytes and grow() can raise it up to 64 bytes. Buffers created without the option are fixed-length and grow() throws a TypeError on them.
No. A shared buffer can only grow. Calling grow() with a smaller size throws a RangeError. This is different from a resizable ArrayBuffer, whose resize() method can also make it smaller.
A typed array created without a length, like new Uint8Array(buffer), follows the buffer, so its length grows too. A typed array created with a fixed length, like new Uint8Array(buffer, 0, 4), keeps that length. Make the choice when you create the view.
Browsers only provide SharedArrayBuffer on cross-origin isolated pages. The server must send the headers Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp. You can check the result with self.crossOriginIsolated. Node.js has no such rule.