SharedArrayBuffer and Atomics Synchronization vs TypedArrays, ArrayBuffer, and Zero-Copy Transfers
SharedArrayBuffer shares memory concurrently across threads, whereas ArrayBuffer transferables move exclusive ownership.
Structured Comparison Matrix
| Dimension | SharedArrayBuffer and Atomics Synchronization | TypedArrays, ArrayBuffer, and Zero-Copy Transfers |
|---|---|---|
| Mental Model | Shared linear memory pages mapped across multiple JS threads, synchronized with hardware atomic operations and futex wait/notify queues. | Contiguous binary buffers (ArrayBuffer) paired with unboxed numeric views (Uint8Array, Float32Array) that transfer across workers in O(1) via ownership handoff. |
| Maturity & Domain | Baseline · Widely · Web APIs Chrome 68+ · Firefox 79+ · Safari 15.2+ | Baseline · Widely · JS Runtime Chrome 7+ · Firefox 4+ · Safari 5.1+ |
| Prerequisites | ||
| When Not to Use | When not to use SharedArrayBuffer: Standard request/response worker tasks work cleanly with TypedArrays & Zero-Copy Transfers via postMessage and avoid COOP/COEP deployment constraints on third-party embeds. | 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). |
Engine Mechanics & Trade-Offs
Multiple JS isolates map the same virtual memory pages and coordinate reads/writes using CPU atomic instructions and OS futex queues.
Key Pitfalls: Calling Atomics.wait() on the main thread: Throws a TypeError synchronously; use Atomics.waitAsync() if the UI thread must observe a shared slot. Data races on non-atomic views: Mixing plain view[0] = 1 with Atomics.load(view, 0) causes torn reads on 64-bit values or stale CPU cache lines.
ArrayBuffer allocates a raw contiguous byte block while TypedArray views index it directly without JS object boxing, enabling O(1) transfer between workers.
Key Pitfalls: 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.