SharedArrayBuffer
In one line
A SharedArrayBuffer is a binary buffer whose memory can be shared between the main thread and Web Workers simultaneously.
In simple words
A SharedArrayBuffer is like an ArrayBuffer, but its underlying memory can be accessed by multiple threads (the main thread and Web Workers) at the same time, without copying. This enables true shared-memory parallelism in JavaScript.
When you postMessage a SharedArrayBuffer, the receiver gets a reference to the same memory, not a copy. Both threads can read and write through their typed array views. Because concurrent access can cause data races, you use the Atomics API to perform synchronized, guaranteed-atomic operations.
SharedArrayBuffer requires a cross-origin-isolated context (specific COOP/COEP headers) for security, so it is not available on all pages. It is used in multithreaded WASM, high-performance computation, and the Threads.js proposal. For most use cases, copying via postMessage is simpler and safer.
Example
// Main thread (requires cross-origin isolation)
const sab = new SharedArrayBuffer(4);
const view = new Int32Array(sab);
const worker = new Worker("worker.js");
worker.postMessage(sab); // shares the same memory
// Atomic, thread-safe write
Atomics.store(view, 0, 42);
console.log(Atomics.load(view, 0)); // 42The SharedArrayBuffer is shared with the worker (no copy). Atomics.store/load perform thread-safe reads and writes.
Related terms
Related Resources
Related Lessons
- Binary Data
Learn shared memory.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.