JavaScript SharedArrayBuffer grow() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Instance method

Definition

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.

JavaScript
const buffer = new SharedArrayBuffer(4, { maxByteLength: 16 });

console.log(buffer.growable);       // true
console.log(buffer.byteLength);     // 4

buffer.grow(8);
console.log(buffer.byteLength);     // 8
console.log(buffer.maxByteLength);  // 16
CallWhat it does
buffer.grow(newLength)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.growableIs true for buffers made with maxByteLength, and false for all others.
buffer.maxByteLengthThe 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

ParameterDescription
newLengthThe 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 ValueDescription
undefinedNothing 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.

JavaScript
const buffer = new SharedArrayBuffer(4, { maxByteLength: 16 });
const tracking = new Uint8Array(buffer);
const fixed = new Uint8Array(buffer, 0, 4);
tracking.set([1, 2, 3, 4]);

const result = buffer.grow(8);

console.log("Returned: " + result);
console.log("byteLength: " + buffer.byteLength);
console.log("Tracking view: " + tracking.join(","));
console.log("Fixed view length: " + fixed.length);
Try It Yourself

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

  1. 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.
  2. 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.
  3. 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(","));
Try It Yourself

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

  1. 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.
  2. 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.
  3. 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.

JavaScript
const growable = new SharedArrayBuffer(8, { maxByteLength: 12 });
const fixed = new SharedArrayBuffer(8);

console.log("growable: " + growable.growable);
console.log("fixed growable: " + fixed.growable);
console.log("maxByteLength: " + growable.maxByteLength);

const tests = [
  ["fixed buffer", () => fixed.grow(16)],
  ["shrink to 4", () => growable.grow(4)],
  ["over the limit", () => growable.grow(64)],
  ["same size", () => growable.grow(8)],
  ["grow to 12", () => growable.grow(12)]
];

for (const [label, test] of tests) {
  try {
    test();
    console.log(label + " -> ok, byteLength " + growable.byteLength);
  } catch (err) {
    console.log(label + " -> " + err.name);
  }
}
Try It Yourself

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

  1. Check growable first. A buffer made without maxByteLength reports growable as false, and its maxByteLength is just its normal size.
  2. 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.
  3. 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.

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.

ES2024 Standard since
Google Chrome 111+ · Desktop & Mobile
Full support
Microsoft Edge 111+ · Chromium based
Full support
Mozilla Firefox 128+ · Desktop & Mobile
Full support
Apple Safari 16.4+ · macOS & iOS
Full support
Opera 97+ · Chromium based
Full support
Samsung Internet 22+ · Android
Supported
Android WebView 111+ · Chromium based
Supported
Node.js 20+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer No 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.

Next: slice()

Learn how to copy a range of bytes into a new SharedArrayBuffer, with negative positions that count from the end.

slice() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful