What is SharedArrayBuffer?

A SharedArrayBuffer is a fixed-size block of raw binary memory that can be shared between the main thread and workers, so they all read and write the same bytes directly instead of copying data through messages.

How it differs from ArrayBuffer

  • postMessage(arrayBuffer) copies the data (or transfers ownership, after which the sender loses access).
  • postMessage(sharedArrayBuffer) shares it: every thread sees changes immediately. You access the bytes through typed arrays (Int32Array, Float64Array, ...) placed over the buffer.

Why use it

  • Very large data that would be expensive to copy (audio and video processing, simulations, image buffers).
  • High-performance parallel algorithms, WebAssembly threads, game engines, emulators.

The catch: race conditions

When several threads write the same memory, normal reads and writes can interleave and lose updates. Use Atomics operations (Atomics.add, Atomics.compareExchange, Atomics.wait/notify) to make updates safe and to synchronise threads.

Security requirements in browsers

After the Spectre CPU vulnerabilities, browsers require the page to be cross-origin isolated before SharedArrayBuffer is available:

  • Cross-Origin-Opener-Policy: same-origin
  • Cross-Origin-Embedder-Policy: require-corp (or credentialless) Check self.crossOriginIsolated. These headers can break third-party embeds and popups, so plan for them.

Node.js

SharedArrayBuffer works with worker_threads without special headers.

When not to use it

Most apps do not need shared memory; message passing (with transferables) is simpler and safer.

Code example

js
1// Shared memory needs cross-origin isolation in browsers
2const available = typeof SharedArrayBuffer !== "undefined" && (globalThis.crossOriginIsolated ?? true);
3console.log("SharedArrayBuffer usable here:", available);
4
5if (available) {
6 const shared = new SharedArrayBuffer(4 * Int32Array.BYTES_PER_ELEMENT); // 16 bytes
7 const view = new Int32Array(shared);
8
9 // Two views over the same memory see the same data (this is what workers would share)
10 const otherThreadView = new Int32Array(shared);
11 otherThreadView[0] = 42;
12 console.log(view[0]); // 42 - no copying
13
14 // Safe concurrent updates use Atomics
15 Atomics.add(view, 1, 5);
16 Atomics.add(view, 1, 5);
17 console.log(Atomics.load(view, 1)); // 10
18
19 // Sending it to a worker shares (not copies) the memory:
20 // worker.postMessage(shared);
21 // inside the worker: const v = new Int32Array(event.data); Atomics.add(v, 1, 1);
22} else {
23 console.log("Serve the page with COOP: same-origin and COEP: require-corp to enable it");
24}
25
26// Compare: a normal ArrayBuffer is copied by structuredClone / postMessage
27const normal = new Int32Array([1, 2, 3]);
28const copy = structuredClone(normal);
29copy[0] = 99;
30console.log(normal[0], copy[0]); // 1 99

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.

Open in study app