JavaScript ArrayBuffer resize() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayBuffer

Definition

buffer.resize(newLength) changes the size of a resizable ArrayBuffer in place. You opt in when you create the buffer: new ArrayBuffer(length, { maxByteLength }). Growing adds zero bytes at the end; shrinking drops bytes from the end. The buffer object stays the same, so views created on it keep working.

Remember
const buf = new ArrayBuffer(4, { maxByteLength: 16 });

buf.resize(8)    → byteLength 8, the 4 new bytes are 0
buf.resize(2)    → byteLength 2, bytes 2..7 are gone
buf.resize(32)   → RangeError (over maxByteLength)

new ArrayBuffer(4).resize(8)  → TypeError (not resizable)

Syntax

Create the buffer with a maxByteLength, then call resize on it.

JavaScript
const buffer = new ArrayBuffer(length, { maxByteLength });
buffer.resize(newLength)
CallWhat it does
buffer.resize(newLength)Set the byte length to newLength, anywhere from 0 up to buffer.maxByteLength.
new ArrayBuffer(length, { maxByteLength })Create a buffer that resize() is allowed to change.

Added in ES2024. Only buffers created with the maxByteLength option can be resized; check buffer.resizable when unsure.

Library declarations
JavaScript
new ArrayBuffer(length, { maxByteLength })   // resizable buffer
buffer.resize(newLength)                      // returns undefined

buffer.resizable       // true for buffers made with maxByteLength
buffer.maxByteLength   // upper limit for resize()
buffer.byteLength      // current size, changes after resize()

Parameters

ParameterDescription
newLengthThe new size in bytes, from 0 to buffer.maxByteLength. Decimals are cut toward zero (2.9 becomes 2); leaving it out means 0.

Return Value

Return ValueDescription
undefinedNothing is returned. The buffer itself changes: read buffer.byteLength to see the new size.

JavaScript Examples

Example 1 — Grow and Shrink in Place

Make a 4-byte resizable buffer, fill it, then grow and shrink it while a Uint8Array watches.

JavaScript
const buffer = new ArrayBuffer(4, { maxByteLength: 16 });
const bytes = new Uint8Array(buffer);
bytes.set([1, 2, 3, 4]);

buffer.resize(8);
console.log(buffer.byteLength);                  // 8
console.log(JSON.stringify(Array.from(bytes)));  // [1,2,3,4,0,0,0,0]

buffer.resize(2);
console.log(buffer.byteLength);                  // 2
console.log(JSON.stringify(Array.from(bytes)));  // [1,2]
console.log(buffer.maxByteLength);               // 16
Try It Yourself

How It Works

  1. Opt in at creation. The maxByteLength: 16 option makes the 4-byte buffer resizable, up to 16 bytes.
  2. Grow with zeros. After resize(8) the old bytes stay and four zero bytes appear. The bytes view was created without a length, so it now shows all 8.
  3. Shrink from the end. resize(2) keeps only the first two bytes. maxByteLength stays 16, so the buffer can grow again later.

Example 2 — A Growable Byte Buffer for a Message

Build a message whose final size is unknown. The buffer doubles its capacity when full, but never past maxByteLength.

JavaScript
const buffer = new ArrayBuffer(4, { maxByteLength: 64 });
const bytes = new Uint8Array(buffer);
let used = 0;

function append(chunk) {
  const needed = used + chunk.length;
  if (needed > buffer.byteLength) {
    const doubled = buffer.byteLength * 2;
    buffer.resize(Math.min(buffer.maxByteLength, Math.max(needed, doubled)));
  }
  bytes.set(chunk, used);
  used += chunk.length;
}

append([72, 73]);         // "HI" header
append([1, 2, 3]);        // payload part 1
append([4, 5, 6, 7, 8]);  // payload part 2

console.log(used);               // 10
console.log(buffer.byteLength);  // 16
console.log(JSON.stringify(Array.from(bytes.subarray(0, used))));
Try It Yourself

How It Works

  1. Track used bytes and capacity. used counts real message bytes; buffer.byteLength is the capacity. Growing ahead of time avoids a resize on every append.
  2. Resize only when full. The second chunk needs 5 bytes, so capacity doubles from 4 to 8. The third needs 10, so it doubles again to 16, still under the 64-byte limit.
  3. No manual copying. The length-tracking bytes view sees the new space immediately, and subarray(0, used) shows just the message.

Example 3 — Check resizable and Catch the Errors

Two common mistakes: resizing a normal fixed-length buffer, and asking for more than maxByteLength.

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

console.log(fixed.resizable);     // false
console.log(growable.resizable);  // true

try {
  fixed.resize(4);
} catch (err) {
  console.log(err.name);          // TypeError
}

try {
  growable.resize(64);
} catch (err) {
  console.log(err.name);          // RangeError
}

console.log(growable.byteLength); // 8
Try It Yourself

How It Works

  1. Read resizable first. Buffers made without maxByteLength report resizable as false and can never change size.
  2. Wrong kind of buffer. Calling resize() on the fixed buffer throws a TypeError. Error messages differ between engines, so the example prints only err.name.
  3. Past the limit. Asking for 64 bytes when the limit is 12 throws a RangeError, and the failed call leaves byteLength at 8.

Browser & Runtime Support

resize(), the maxByteLength option, and the resizable property are part of ES2024.

✓ Newer · ES2024

ArrayBuffer.prototype.resize()

In recent Chrome, Edge, Firefox, Safari, and Opera, and in Node.js 20 or newer (the examples were run on Node.js 24). First versions differ per browser, so check a compatibility table such as MDN's for your targets.

Recent Recent browsers only
Google Chrome Recent versions
Supported
Mozilla Firefox Recent versions
Supported
Apple Safari Recent versions
Supported
Microsoft Edge Recent versions
Supported
Node.js Node.js 20+ · check with typeof
Supported
Deno Recent versions
Supported
Internet Explorer No support
Not supported
resize() Recent browsers

Bottom line: Feature-detect with typeof ArrayBuffer.prototype.resize === "function". Fallback: allocate a bigger buffer and copy with new Uint8Array(bigger).set(new Uint8Array(old)).

Frequently Asked Questions

Pass the maxByteLength option to the constructor: new ArrayBuffer(8, { maxByteLength: 64 }). The buffer starts at 8 bytes and resize() can change it to anything from 0 to 64. Buffers created without the option are fixed-length.
Not in a way your code can see. The same ArrayBuffer keeps its bytes and changes its length, so there is no new buffer to allocate and fill by hand.
A typed array created without a length, like new Uint8Array(buffer), tracks the buffer and its length follows every resize. A typed array with a fixed length that no longer fits after shrinking reports length 0 until the buffer grows again.
A TypeError means the buffer is not resizable or was detached by transfer(). A RangeError means the new length is negative or larger than maxByteLength. Check buffer.resizable and buffer.maxByteLength before calling it.
Resizable ArrayBuffers are ES2024. Recent Chrome, Edge, Firefox, and Safari support them, as does Node.js 20 or newer. Feature-detect with typeof ArrayBuffer.prototype.resize === "function".

Next: slice()

Copy a range of bytes into a brand-new buffer, with Array-style negative indexes.

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.

11 people found this page helpful