JS RuntimeBaseline · Widely

TypedArrays, ArrayBuffer, and Zero-Copy Transfers

Mental Model

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.

Interactive Simulator

Boxed JS Array Copy vs Zero-Copy Transferable ArrayBuffer

Lines of Code
Before
3
After
3
Reflows / Browser Cost
Before
O(N) copy + GC pressure
After
O(1) pointer move
BeforeBoxed JS Array + Structured Copy
const pixels = new Array(1_000_000).fill(0);
worker.postMessage(pixels);
// Serializes 1M boxed numbers on main thread (~45ms)
Lines: 3Reflows: O(N) copy + GC pressureDependencies: Boxed heap elements

Duplicates megabytes of memory and blocks the sender thread.

After (Native)Uint8ClampedArray + Zero-Copy Transfer
const pixels = new Uint8ClampedArray(1_000_000);
worker.postMessage(pixels.buffer, [pixels.buffer]);
// Pointer handoff in O(1); sender byteLength becomes 0
Lines: 3Reflows: O(1) pointer moveDependencies: Contiguous C++ buffer

Transfers 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).

  1. Buffer vs View: ArrayBuffer holds raw bytes; Uint8Array or Float32Array slices a typed window (byteOffset, byteLength) over it.
  2. Zero Boxing: Unlike Array, elements never allocate heap wrappers or stress Garbage Collection.
  3. Transferable Move: Listing buffer in postMessage(msg, [buffer]) detaches the sender's view (byteLength === 0) and hands the pointer to the worker.
JS
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, and DataView into the core ECMAScript spec.
  • 2023 (ArrayBuffer.prototype.transfer): Added explicit in-place resize and detach semantics on the main thread.

Production Pitfalls

Production Pitfall
  • Host Endianness Trap: Uint32Array reads bytes in host CPU order (little-endian on x86/ARM), silently scrambling big-endian network headers.
  • Use-After-Transfer Crash: Reading a TypedArray after transferring its ArrayBuffer throws TypeError or returns empty slices.
When Not to Use
  • When not to use TypedArray: Avoid TypedArray for small, sparse, or mixed-type lists, creating a backing ArrayBuffer costs more than a small literal [1, 2, 3]; for hashing or encryption over binary buffers, pass the ArrayBuffer directly to Web Crypto (SubtleCrypto).

Connections & Logical Next Steps

Seen in Architecture Autopsies

Mechanical Comparisons