Skip to solution
hardDSA

How does ArrayBuffer transfer and detach work with `transfer()` vs `transferToFixedLength()`?

608 views
01

Understand the problem

Question presented to candidate: "You have an 8-byte ArrayBuffer that a video-processing pipeline needs to hand off to a Web Worker without copying the bytes. Walk me through what buf.transfer() actually does to the original buffer, how it differs from transferToFixedLength(), and what happens if code somewhere still holds a TypedArray view over the original buffer after the transfer."

What a strong answer should cover:

  • transfer() moves the underlying memory to a brand-new ArrayBuffer and detaches the original in place — the original's byteLength becomes 0 and its detached property becomes true, with no bytes actually copied.
  • A TypedArray view over a detached buffer does not throw on simple reads: .length and .byteLength silently report 0, and an index read returns undefined. Only a WRITE, such as .set(), throws a TypeError.
  • transfer(newLength) can also grow (zero-filled) or shrink the buffer as part of the same call — a separate resize() step is not required.
  • transferToFixedLength() always produces a non-resizable result, even when the source was a resizable ArrayBuffer created with maxByteLength — this is the one concrete behavioral difference from plain transfer(), which preserves the source's resizable-ness and maxByteLength.
  • Calling transfer() a second time on an already-detached buffer throws a TypeError rather than silently no-oping.
  • This is the same underlying detach mechanism postMessage/structuredClone have used for years via an explicit transfer list — these methods just expose it as a direct, synchronous API instead of requiring a message-passing round trip.

Clarifying questions expected:

  • "Does the receiving side need the resizable-ness of the buffer preserved, or is a fixed-length copy acceptable?" — this determines transfer() vs transferToFixedLength().
  • "Is there code elsewhere holding a TypedArray view over the original buffer that needs a defensive check?" — every existing view over the original becomes a permanently zero-length view immediately after transfer.

Code / implementation expected: Yes — real, executed calls to transfer() and transferToFixedLength() on both plain and resizable ArrayBuffers, showing the actual detached state and a real thrown error, not just a description of the spec.

arraybuffertransfermemory
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript memory-management interview questions. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result shown below is real, captured output from actually running

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSArrayBuffer transfer(), transferToFixedLength(), detach, and error behavior, all genuinely executed (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 141 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track