# SharedArrayBuffer and Atomics Synchronization vs TypedArrays, ArrayBuffer, and Zero-Copy Transfers

- **Canonical URL:** https://frontend.rocks/vs/shared-array-buffer-atomics-vs-typed-arrays-zero-copy/
- **Relation:** Contrasts With
- **Last Verified:** 2026-11-15
- **Verdict:** SharedArrayBuffer shares memory concurrently across threads, whereas ArrayBuffer transferables move exclusive ownership.

## Comparison Matrix

| Dimension | SharedArrayBuffer and Atomics Synchronization | TypedArrays, ArrayBuffer, and Zero-Copy Transfers |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Baseline · Widely |
| **Domain** | Web APIs | JS Runtime |
| **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. |
| **Prerequisites** | Dedicated Web Workers and Transferables (SharedArrayBuffer exists to share raw binary memory concurrently with Web Workers.) | Structured Clone Algorithm (Transferable ArrayBuffers use the Structured Clone transfer list to move ownership without copying.); JavaScript Generational Garbage Collection (Pre-allocated TypedArray views eliminate per-element heap boxing and Young Generation GC churn.) |
| **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). |

## SharedArrayBuffer and Atomics Synchronization — Under the Hood
`SharedArrayBuffer` turns separate [Dedicated Web Workers](/concepts/dedicated-web-workers) from isolated offices into surgeons sharing one operating table: without `Atomics` locks, two hands reaching for the same byte corrupt the state.

1. **Isolation Gate**: Engines expose `SharedArrayBuffer` only when [Cross-Origin Isolation](/concepts/cross-origin-isolation-coop-coep) is active (`self.crossOriginIsolated === true`).
2. **Memory Ordering**: Plain `TypedArray` writes may reorder across CPU cores; `Atomics.store`, `Atomics.load`, and `Atomics.compareExchange` emit hardware memory barriers.
3. **Thread Parking**: `Atomics.wait(int32View, idx, expected)` suspends a worker thread at zero CPU cost until another agent calls `Atomics.notify`.

```js
// Worker thread: block until slot 0 changes from 0
const view = new Int32Array(sharedBuffer);
Atomics.wait(view, 0, 0);
const payloadLength = Atomics.load(view, 1);

// Main thread: write payload length, flip flag, wake worker
Atomics.store(view, 1, 1024);
Atomics.store(view, 0, 1);
Atomics.notify(view, 0, 1);
```

## TypedArrays, ArrayBuffer, and Zero-Copy Transfers — 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](/concepts/js-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)
```

## Interactive Labs & Dissections
- [SharedArrayBuffer and Atomics Synchronization Lab](https://frontend.rocks/concepts/shared-array-buffer-atomics/#lab) ([Markdown](https://frontend.rocks/concepts/shared-array-buffer-atomics.md))
- [TypedArrays, ArrayBuffer, and Zero-Copy Transfers Lab](https://frontend.rocks/concepts/typed-arrays-zero-copy/#lab) ([Markdown](https://frontend.rocks/concepts/typed-arrays-zero-copy.md))

## Related Architecture Autopsies
- [Autopsy #12: Moving the Entire App Engine Off the Main Thread](https://frontend.rocks/autopsies/zero-main-thread-ui-worker/) ([Markdown](https://frontend.rocks/autopsies/zero-main-thread-ui-worker.md)): Rich spreadsheet and graph app drops to 4 FPS during 100k-row sorts until state and OPFS move to a Worker.

## Primary Sources
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
- https://tc39.es/ecma262/#sec-atomics-object
- https://tc39.es/ecma262/#sec-typedarray-objects
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Typed_arrays
