Sub topic 14
Resizable Buffers and ArrayBuffer.prototype.transfer
This chapter assumes you already know how to create an ArrayBuffer, put typed array views over it, and use byteOffset and length. Everything here is part of ES2024, so you need a current browser or Node.js 20 or later.
Why a Fixed Size Gets in the Way
A classic ArrayBuffer keeps the size it was created with. If you are collecting bytes from a stream and run out of space, you have to allocate a bigger buffer, copy the old bytes into it, and recreate every view. ES2024 gives you two tools for this:
- A resizable buffer can grow or shrink in place, up to a limit you choose when you create it.
transfer()moves a buffer's contents into a new buffer and detaches the old one. A detached buffer has a length of zero and can no longer be used.
Creating and Resizing a Buffer
To make a buffer resizable, pass a maxByteLength option. The engine may reserve address space up to that limit, so pick a realistic upper bound.
const buf = new ArrayBuffer(4, { maxByteLength: 16 });
const view = new Uint8Array(buf); // no length given, so it tracks the buffer
console.log(buf.resizable, view.length); // true 4
buf.resize(12);
console.log(view.length); // 12
console.log(view[11]); // 0, because new bytes are zero-filled
const moved = buf.transfer();
console.log(buf.detached, buf.byteLength, moved.byteLength); // true 0 12A view created without a length argument is length-tracking. Its length follows the buffer each time the buffer is resized. A view created with an explicit length keeps that length.
Fixed-Length Views on a Shrinking Buffer
When you shrink a buffer, a fixed-length view can end up pointing past the end of the buffer. The view does not throw an error. It reports a length of 0 until the buffer grows large enough to hold it again:
const buf = new ArrayBuffer(8, { maxByteLength: 16 });
const fixed = new Uint8Array(buf, 0, 8);
buf.resize(4);
console.log(fixed.length, fixed[0]); // 0 undefined
buf.resize(8);
console.log(fixed.length); // 8, back in boundstransfer and transferToFixedLength
transfer(newLength) returns a new buffer holding the old contents. If you pass newLength, the contents are truncated or zero-padded to that size. The new buffer is resizable only if the original was resizable. transferToFixedLength(newLength) always returns a non-resizable buffer.
| Method | Result resizable? | Old buffer |
|---|---|---|
| `transfer()` | Same as the original | Detached |
| `transfer(n)` | Same as the original, n bytes | Detached |
| `transferToFixedLength(n)` | No | Detached |
| `slice()` | No | Unchanged (copy) |
Use transfer to hand off ownership. After the call, any code still holding the old buffer or its views sees length 0, so stale references cannot change data that has moved on.
Common Mistakes
- Giving a view an explicit length and then expecting it to grow along with the buffer.
- Calling
resize()on a buffer that has already been transferred. It throws a TypeError. - Setting
maxByteLengthfar larger than you need, which wastes reserved memory.
Exercise
You create const b = new ArrayBuffer(2, { maxByteLength: 8 }); const v = new Uint16Array(b); then call b.resize(6) and const c = b.transfer(4);. What are v.length, b.byteLength, c.byteLength, and c.resizable?
Show answer
v.length is 0 because b is detached (it was 3 just before the transfer). b.byteLength is 0. c.byteLength is 4 because the contents were truncated. c.resizable is true because transfer keeps the original's resizability.
Exercise
Why does new Uint8Array(buf, 0, 8) report length 0 after buf.resize(4), instead of throwing an error?
Show answer
The fixed-length view is now out of bounds. Out-of-bounds views report length 0 and read as undefined. They become usable again if the buffer grows back to cover them.