Skip to the solution
HardDSA6 min read

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

608 views

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.

  • arraybuffer
  • transfer
  • memory

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 7 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

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

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 141 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

Temporal in practice: how do you correctly handle time zones, DST and calendar arithmetic?

Continue