`Atomics` and `SharedArrayBuffer`
SharedArrayBuffer is a block of memory two agents can see at once. Atomics provides reads, writes, waits, and notifies on that memory that cannot tear and that establish ordering.
Detailed Explanation
Structured clone copies. Two workers that need a lock or a ring buffer of pixels would pay for a copy every frame. SharedArrayBuffer is the same bytes in both heaps. A Int32Array view over it is how you read numbers. Without Atomics, a write of a 32-bit value can race with a read; more importantly, compilers and CPUs can reorder so one side never sees the other's flag.
Atomics.store, load, add, sub, and, or, xor, exchange, compareExchange operate on integer typed-array views (Int32Array is the usual). They are atomic at the element width. Atomics.wait (blocking, worker-only in browsers for Int32) sleeps until Atomics.notify wakes it, or a timeout. The main browser thread must not wait; it would freeze the page. Use Atomics.waitAsync there.
Shared memory is a data-race surface. JavaScript's ordinary objects are not shared across workers. This API is the exception. You are writing concurrent C-like code: define who writes which index, use a lock word (compareExchange 0→1), or a single-producer single-consumer cursor pair with Atomics.
Browsers require cross-origin isolation (Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy) before SharedArrayBuffer is available, because Spectre-class attacks used high-resolution shared memory. crossOriginIsolated tells you if you have it. Node can create SharedArrayBuffer for worker threads without those headers.
What it costs you: unsafety and ops burden. A missed Atomics on a flag is a Heisenbug. COOP/COEP can break third-party iframes and assets. Prefer postMessage unless you have a measured need to share bytes.
Reach for this when a worker and the page (or two workers) update a buffer at high frequency: audio, video frames, physics. Reach for messages when you send jobs and results a few times a second. Reach for Atomics.notify instead of busy-spinning.
Code Examples
const sab = new SharedArrayBuffer(8);
const view = new Int32Array(sab);
Atomics.store(view, 0, 1);
console.log(Atomics.load(view, 0)); // => 1
console.log(Atomics.add(view, 0, 2)); // => 1 (previous)
console.log(Atomics.load(view, 0)); // => 3function tryLock(view, index) {
return Atomics.compareExchange(view, index, 0, 1) === 0;
}
function unlock(view, index) {
Atomics.store(view, index, 0);
Atomics.notify(view, index, 1);
}
const lock = new Int32Array(new SharedArrayBuffer(4));
console.log(tryLock(lock, 0)); // => true
console.log(tryLock(lock, 0)); // => false
unlock(lock, 0);console.log(typeof crossOriginIsolated === "boolean" ? crossOriginIsolated : "n/a");Common Gotchas
Do not `Atomics.wait` on the browser main thread
it blocks painting. Use `waitAsync` or wait in a worker.
`SharedArrayBuffer` is gated in browsers
without COOP/COEP, constructing one throws or returns a detached/unavailable path. Check `crossOriginIsolated`.
Ordinary property reads on the view are not atomic enough for flags
use `Atomics.load` / `store` for coordination words.
Only integer typed arrays
no `Atomics` on `Float64Array` for wait/notify. Use `Int32` control words beside float payloads.