skip to content

In ES2024 JavaScript, code keeps appending bytes to one ArrayBuffer; how do you choose between a resizable buffer, transfer(newByteLength) and copying, and what happens to existing views under each?

level: seniorimportance: nice to knowfreq 16%

answer

  1. ceiling known up front or not
  2. views with no explicit length
  3. out of bounds reads as empty
  4. detached means zero and throwing
  5. copying leaves stale views alive

basics

~20 s

Use a resizable ArrayBuffer when a maximum is known, since views with no explicit length follow it. Use transfer(newByteLength) when none is: the old buffer detaches and its views go empty. Copy only when the old bytes must stay usable.

solid answer

~50 s

Since ES2024 there are three options. A resizable buffer, `new ArrayBuffer(n, { maxByteLength })`, grows in place with `resize()`. Views created without a length track it, views with a fixed length keep their size, and shrinking below a view's range makes it read as length 0. The cost is committing to a ceiling up front, and exceeding it throws `RangeError`. `transfer(newByteLength)` needs no ceiling: it returns a new buffer with the same bytes, possibly without a real copy, and detaches the old one, so old views report length 0 and their methods throw `TypeError`. Every holder of a view must then rebuild it. Allocating a bigger buffer and copying with `set()` leaves the old buffer and its views alive but stale, so writes silently diverge. I would pick resizable when a sensible maximum is known and transfer otherwise, and copy only when the old bytes must stay usable.

code

javascript · 12 lines
javascript
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });
const tracking = new Uint8Array(buffer);
const fixed = new Uint8Array(buffer, 0, 8);

buffer.resize(12);
console.log(tracking.length, fixed.length); // 12 8

buffer.resize(4);
console.log(tracking.length, fixed.length); // 4 0

buffer.resize(8);
console.log(tracking.length, fixed.length); // 8 8

go deeper

for a junior

Recall that an ordinary ArrayBuffer has a fixed size, and that ES2024 added maxByteLength and resize for buffers that can change. Know that transfer leaves the old buffer unusable.

for a middle

Explain length-tracking against fixed-length views, what out of bounds looks like, and what a detached buffer and its views report. Predict lengths after a grow and after a shrink.

for a senior

Make the choice from the constraints: is a ceiling known, who holds views, and must old bytes stay usable. Name the failure each option produces, including stale views that fail silently after a copy.

for a principal

The tradeoff angle is the contract between modules. Decide whether a shared buffer may move underneath its holders; a resizable buffer keeps views valid, while transfer forces an explicit handover that every owner must follow.

## Three strategies Before ES2024, an `ArrayBuffer` had a fixed size for life, so growing one meant allocating a larger buffer and copying. ES2024 added two alternatives, and they differ mostly in what happens to the views other code already holds. | Strategy | Needs a ceiling | Existing views | Old buffer | |---|---|---|---| | Resizable buffer with `resize()` | yes, `maxByteLength` | tracking views follow, fixed views stay | is the same buffer | | `transfer(newByteLength)` | no | length 0, methods throw | detached | | Allocate and copy with `set()` | no | still work, on old data | still usable | ## Resizable buffers and length-tracking views Passing `maxByteLength` to the constructor makes a buffer **resizable**. `resize(n)` then changes its size to any value up to that ceiling, and new bytes are zero. Asking for more than the ceiling throws `RangeError`, and calling `resize()` on an ordinary buffer throws `TypeError`. What a view does depends on how it was created: - A typed array built with **no `length` argument** is **length-tracking**, and follows the buffer as it grows and shrinks. - A typed array built with an **explicit `length`** keeps that size when the buffer grows. - A `DataView` built without `byteLength` tracks the buffer too. ```js const buffer = new ArrayBuffer(8, { maxByteLength: 16 }); const tracking = new Uint8Array(buffer); const fixed = new Uint8Array(buffer, 0, 8); buffer.resize(12); console.log(tracking.length, fixed.length); // 12 8 ``` A tracking view stays the same object, so no code has to be told about the resize, provided it reads `length` when it needs it instead of caching an old value. It covers whole elements only: a tracking `Uint32Array` over a buffer resized to 6 bytes has `length` 1, and the 2 spare bytes are not visible through it. ## Shrinking and out-of-bounds views Shrinking below a view's range makes the view **out of bounds**: its `length` and `byteLength` read as 0 and indexes give `undefined`. This is not permanent. Growing the buffer back restores the view's size, but not the dropped bytes, since regrown bytes are zero: ```js const b = new ArrayBuffer(8, { maxByteLength: 16 }); const f = new Uint8Array(b, 0, 8); f[7] = 5; b.resize(4); console.log(f.length); // 0 b.resize(8); console.log(f.length, f[7]); // 8 0 ``` A length-tracking view that starts at a non-zero `byteOffset` goes out of bounds the same way, when the buffer shrinks below that offset. ## transfer() and detachment `buffer.transfer(newByteLength)` returns a new `ArrayBuffer` with the same bytes and **detaches** the original. Extra bytes are zero, and a smaller size drops the overflow. An engine may implement it as a zero-copy move or a `realloc`, so it can be much cheaper than a manual copy. After it: - `buffer.detached` is `true` and `buffer.byteLength` is 0. - Old typed array views have `length` 0, read `undefined`, and ignore index writes. - Old views' methods, such as `fill`, throw `TypeError`, and so do `slice`, `resize` and another `transfer` on the old buffer. `transfer()` preserves resizability: a resizable source yields a resizable result with the same `maxByteLength`, and asking for more than that throws `RangeError`. `transferToFixedLength()` always yields a non-resizable buffer. Nothing turns a fixed-length buffer into a resizable one by transfer. Moving a buffer to another thread leaves the sender in the same detached state, a subject that belongs with browser workers. ## Allocate and copy `new Uint8Array(newBuffer).set(new Uint8Array(oldBuffer))` still works everywhere, and `ArrayBuffer.prototype.slice` makes a copy without detaching anything. Old and new buffers both exist during the copy. Old views stay valid and keep reading the old bytes, which is a bug when anything writes to the new buffer and expects them to see it, because nothing fails. ## How to choose 1. **Is there a sensible ceiling?** If so, a resizable buffer is the simplest, since views keep working. Keep `maxByteLength` as small as the use allows; the usual guidance is never to exceed 1 GB, to limit the risk of out-of-memory errors. 2. **No ceiling, and you control the views?** Use `transfer()`. You must recreate every view, and detachment makes a stale one fail visibly when its methods are called, though plain index reads only give `undefined`. 3. **Must the old bytes stay usable?** Copy, or take a snapshot with `slice()`. 4. **Must you support runtimes older than ES2024?** Copy, or feature-test for `resize` and `transfer` first.

  • What is the difference between `transfer()` and `transferToFixedLength()`?
    `transfer()` keeps resizability: a resizable source gives a resizable result with the same `maxByteLength`, and `newByteLength` may not exceed that limit. `transferToFixedLength()` always gives a non-resizable buffer, so its `newByteLength` can be larger than the source's `maxByteLength`. Both detach the source, and neither can make a fixed-length buffer resizable.
  • How does `ArrayBuffer.prototype.slice` differ from `transfer()`?
    `slice(start, end)` copies a byte range into a new buffer and leaves the original untouched, so existing views keep working. `transfer()` detaches the original and may move its memory with no real copy. Use `slice` for a snapshot you want alongside the original, and `transfer` when the original should stop being used.
  • What happens if you write through a view after its buffer has been detached?
    An index write such as `view[0] = 1` does nothing and throws no error, since a detached view has length 0 and every index is out of range. Reads give `undefined`. Methods such as `fill` do throw `TypeError`, so a stale view is noisy only if the code happens to call a method.

  • A typed array created with an explicit length grows along with its resizable buffer.
  • After transfer(), the old buffer still holds a readable copy of the bytes.
  • resize() works on any ArrayBuffer, since every buffer can be reallocated.
  • transfer() always makes a physical copy of the bytes.
  • transfer() needs a worker or a second thread to be involved.
  • A view pushed out of bounds by shrinking can never work again.

Assumes ES2024, which added resizable ArrayBuffers (maxByteLength, resize) and ArrayBuffer.prototype.transfer; earlier editions offer only allocate-and-copy.