TypedArrays, ArrayBuffer, and Zero-Copy Transfers
An ArrayBuffer allocates a raw, contiguous byte block outside the boxed JS object heap, while TypedArray views (Uint8Array, Float32Array) read unboxed numbers straight from memory. Passing buffer in postMessage's transfer list moves ownership in O(1) instead of copying via Structured Clone.
Boxed JS Array Copy vs Zero-Copy Transferable ArrayBuffer
const pixels = new Array(1_000_000).fill(0);
worker.postMessage(pixels);
// Serializes 1M boxed numbers on main thread (~45ms)Duplicates megabytes of memory and blocks the sender thread.
const pixels = new Uint8ClampedArray(1_000_000);
worker.postMessage(pixels.buffer, [pixels.buffer]);
// Pointer handoff in O(1); sender byteLength becomes 0Transfers ownership to the worker in microseconds with zero copy.
Under the Hood
Picture handing over the keys to a locked storage unit instead of unpacking every box into a second truck: the recipient gets instant access, and your copy of the key snaps in half (detached).
- Buffer vs View:
ArrayBufferholds raw bytes;Uint8ArrayorFloat32Arrayslices a typed window (byteOffset,byteLength) over it. - Zero Boxing: Unlike
Array, elements never allocate heap wrappers or stress Garbage Collection. - Transferable Move: Listing
bufferinpostMessage(msg, [buffer])detaches the sender's view (byteLength === 0) and hands the pointer to the worker.
const raw = new ArrayBuffer(16);
const view = new DataView(raw);
view.setUint32(0, 0xdeadbeef, false); // Explicit big-endian
// Zero-copy handoff to worker
worker.postMessage(raw, [raw]);
console.log(raw.byteLength); // 0 (detached)Problem Genealogy
- 2011 (WebGL TypedArrays): Introduced contiguous binary buffers so JavaScript could feed GPU vertex and texture memory without boxing.
- 2015 (ES6 Standardization): Unified
ArrayBuffer,TypedArray, andDataViewinto the core ECMAScript spec. - 2023 (
ArrayBuffer.prototype.transfer): Added explicit in-place resize and detach semantics on the main thread.
Production Pitfalls
- Host Endianness Trap:
Uint32Arrayreads bytes in host CPU order (little-endian on x86/ARM), silently scrambling big-endian network headers. - Use-After-Transfer Crash: Reading a
TypedArrayafter transferring itsArrayBufferthrowsTypeErroror returns empty slices.
- When not to use
TypedArray: AvoidTypedArrayfor small, sparse, or mixed-type lists, creating a backingArrayBuffercosts more than a small literal[1, 2, 3]; for hashing or encryption over binary buffers, pass theArrayBufferdirectly to Web Crypto (SubtleCrypto).
Connections & Logical Next Steps
Large state diffs transfer across worker mailboxes via Transferable ArrayBuffers without copying.
TypedArrays view WebAssembly.Memory.buffer directly so JS and Wasm exchange binary batches without copying.
Transferable ArrayBuffers use the Structured Clone transfer list to move ownership without copying.
Pre-allocated TypedArray views eliminate per-element heap boxing and Young Generation GC churn.
TypedArrays transfer raw buffers to workers for custom math, whereas crypto.subtle processes ArrayBuffers natively off-thread.
SharedArrayBuffer shares memory concurrently across threads, whereas ArrayBuffer transferables move exclusive ownership.
Seen in Architecture Autopsies
Mechanical Comparisons
SharedArrayBuffer shares memory concurrently across threads, whereas ArrayBuffer transferables move exclusive ownership.
TypedArrays transfer raw buffers to workers for custom math, whereas crypto.subtle processes ArrayBuffers natively off-thread.