← blog
JavaScriptDecember 2, 2026 · 7 min read

I Kept Copying Buffers to Grow Them. Resizable ArrayBuffer Fixed It.

Every "growable buffer" helper allocates bigger, copies everything in, and throws the old buffer away — on a 200MB export that copy is a visible stutter. Resizable ArrayBuffer grows in place, and transfer() hands off ownership without a copy or a postMessage side effect.

Parsa Jiravand · Frontend engineer · building bestpractic
I Kept Copying Buffers to Grow Them. Resizable ArrayBuffer Fixed It.

The "export everything" button had been fine for months. Then someone with eighteen months of notes in the app hit it, and the progress bar started hitching — not failing, just stuttering, every few seconds, like clockwork.

No errors in the console. No dropped frames in a normal sense. Just a build-up feature that got measurably janky exactly in proportion to how much data someone had.

The export assembles a single file from chunks — attachments, encoded notes, whatever — arriving over time from IndexedDB, in pieces whose total size you don't know up front. The standard fix for "I need one contiguous buffer but don't know the final size" looks like this:

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function createGrowableBuffer(initialCapacity = 4096) { let buffer = new ArrayBuffer(initialCapacity); let view = new Uint8Array(buffer); let length = 0; function append(bytes) { if (length + bytes.length > buffer.byteLength) { const bigger = new ArrayBuffer(Math.max(buffer.byteLength * 2, length + bytes.length)); new Uint8Array(bigger).set(view.subarray(0, length)); buffer = bigger; view = new Uint8Array(buffer); } view.set(bytes, length); length += bytes.length; } return { append, bytes: () => view.subarray(0, length) }; }

This is the pattern you'll find in most hand-rolled binary buffer builders, and it isn't wrong, exactly — doubling capacity is the standard amortized-growth trick, the same one Array, String concatenation, and Vec in other languages all use internally. It's also exactly why this one stutters.

Before you keep reading: what does every call to append actually cost, once the buffer is full?

A brand-new ArrayBuffer, allocated at double the size, and a full copy of every byte appended so far into it — then the old buffer becomes garbage. Early on that's nothing: copying 8KB is instant. But capacity doubles each time, so the copies get bigger exactly as the export gets bigger. On a 200MB file built from 4KB chunks, the last couple of growth steps are each copying 50-100MB on the main thread, synchronously, in one memcpy-sized function call that blocks everything else — a single-threaded set() call doesn't yield partway through. That's the stutter: not a bug, just the doubling trick doing exactly what it's designed to do, at a size where "exactly what it's designed to do" is expensive.

The real constraint was never the algorithm — it's that a regular ArrayBuffer is a fixed-size, fixed-location block of memory from the moment it's created. "Growing" one has only ever meant creating a different, bigger one and copying into it, because the original can't get bigger where it already lives.

As of mid-2024, that's no longer the only option. ArrayBuffer can now be created resizable, with a declared ceiling:

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function createGrowableBuffer(initialCapacity = 4096, maxCapacity = 512 * 1024 * 1024) { const buffer = new ArrayBuffer(initialCapacity, { maxByteLength: maxCapacity }); const view = new Uint8Array(buffer); // no offset/length given → tracks the buffer's size automatically let length = 0; function append(bytes) { if (length + bytes.length > buffer.byteLength) { buffer.resize(Math.min(maxCapacity, Math.max(buffer.byteLength * 2, length + bytes.length))); } view.set(bytes, length); length += bytes.length; } return { append, bytes: () => view.subarray(0, length) }; }

buffer.resize() grows (or shrinks) the same allocation — no new ArrayBuffer, no copy of the existing bytes, no garbage for the collector to clean up later. The Uint8Array built without an explicit length is "length-tracking": it automatically reflects however big the underlying buffer currently is, so there's no need to rebuild the view after every resize the way the old version had to.

Runs right in your browser — poke at it and watch the concept react live.

The ceiling is real, not a suggestion — ask it to grow past maxByteLength and it refuses:

JavaScript
1
2
const buffer = new ArrayBuffer(1024, { maxByteLength: 4096 }); buffer.resize(8192); // throws — 8192 is past the 4096 you declared up front

You have to decide the maximum size up front, the same way you'd pass a capacity hint to a Vec or a StringBuilder in another language. For the export button, that's "how big could this reasonably ever get" — a few hundred MB, say — not an unbounded number. That's a feature, not friction: a buffer that could grow to fill all of memory because nobody set a ceiling is its own bug.

There was a second, stranger report buried in the same feature: occasionally, a background "compress before download" step — handed off to a worker — would silently produce an empty file. Not an error. Just zero bytes, like the buffer had never had anything in it.

The cause was a few files away from the compression code: a different part of the export flow held a reference to the same raw buffer and had already posted it to another worker as a transferable:

JavaScript
1
2
3
worker.postMessage(rawBuffer, [rawBuffer]); // elsewhere, no error, no warning: console.log(rawBuffer.byteLength); // 0

postMessage's transfer list has always worked this way — listing an ArrayBuffer there detaches it from the sender, handing the actual memory to the receiver with no copy. That's efficient, and it's also completely invisible at the call site that gets bitten by it: nothing about console.log(rawBuffer.byteLength) looks wrong, three files away from the postMessage call that zeroed it out.

ArrayBuffer.prototype.transfer() does the same ownership hand-off, but as a method you call directly, anywhere — not only across a postMessage boundary:

JavaScript
1
2
3
4
5
6
7
8
function handOff(buffer) { const owned = buffer.transfer(); // new ArrayBuffer, same bytes where possible, no copy console.log(buffer.detached); // true — immediately, at the call site that caused it return owned; } const exported = handOff(rawBuffer); worker.postMessage(exported, [exported]);

Nothing here is new memory-safety machinery — detaching a buffer and handing its bytes to a new owner is exactly what the transfer list already did. What's new is that it's no longer something that only happens as a side effect of the second argument to postMessage. buffer.transfer() reads, in the code that calls it, as "I am done owning this" — so when something downstream still expects rawBuffer to have bytes in it, the bug is one grep away from the line that changed it, not a fact about a worker boundary you have to hold in your head.

(If you need the result to definitely not be resizable afterward — handing a buffer off to code that assumes a fixed size — transferToFixedLength() does the same move and strips the resizability.)

The buffer builder resizes in place instead of reallocating, so the stutter that scaled with export size is gone — there's no longer a growth step that costs a 100MB copy, because there's no copy at all. And the empty-download bug turned into something grep-able: every place a buffer changes hands now says so, in a method name, instead of hiding in a transfer list.

Neither fix needed a library. resize(), transfer(), and the maxByteLength option are Baseline — newly available across Chrome, Firefox, and Safari since 2024 (transfer() reached Baseline first, in March 2024; resizable buffers followed that July, once Firefox shipped resize() too) — so if your target browsers are current, this is just how ArrayBuffer works now. If you still support something older, feature-detect before you lean on it:

JavaScript
const hasResizableBuffers = typeof ArrayBuffer.prototype.resize === "function";

If you've got a doubling-buffer builder living in a binary parser or an export feature somewhere, it's probably not causing a problem yet — it's just waiting for someone with enough data to feel the copy. Have you been bitten by a buffer that went quietly empty after a postMessage call you weren't looking at?

Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.


🚀 Want more like this? Every guide, playground, and quiz lives on bestpractic.org — open it and sign up free so the next one finds you.

Thanks for reading! Let's stay connected:

Keep reading

One post a day, in your inbox

Each one with a runnable playground and a quiz. No pitch, no digest, unsubscribe in one click.

0 comments

Sign in to join the discussion, like comments, and save articles for later.