SharedArrayBuffer
The SharedArrayBuffer global object represents a block of raw binary memory that several threads can read and write at the same time. Like an ArrayBuffer, it holds bytes that code reaches through a view, such as a typed array or a DataView. Every view over it sees the same memory:
> const shared = new SharedArrayBuffer(8);
undefined
> const writer = new Int32Array(shared);
undefined
> const reader = new Int32Array(shared);
undefined
> writer[0] = 42;
42
> reader[0]
42
Sending a SharedArrayBuffer to a worker, for example with postMessage(), doesn’t copy or detach it. The worker gets a new SharedArrayBuffer object backed by the same bytes. Threads coordinate their reads and writes through the Atomics object. A SharedArrayBuffer can’t be detached or transferred, so every thread keeps working on the same bytes:
SharedArrayBuffer was added in ES2017. Growable buffers, created with the maxByteLength option, were added in ES2024 and are Baseline Newly available since 2024.
Note: Browsers hide the SharedArrayBuffer global unless the page is served from a secure context and is cross-origin isolated, which requires the Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy HTTP headers. Node.js always provides it.
Syntax
The SharedArrayBuffer constructor takes a size in bytes and an optional options object:
new SharedArrayBuffer(<length>[, <options>])
| Parameter | Description |
|---|---|
length |
The size of the buffer in bytes. A negative size, or one larger than maxByteLength, throws a RangeError |
options |
An optional object. Its maxByteLength property makes the buffer growable up to that many bytes |
The constructor returns a new SharedArrayBuffer with every byte set to zero. The new keyword is required, and calling SharedArrayBuffer() without it throws a TypeError.
Key Instance Methods and Properties
| Member | Kind | Description |
|---|---|---|
.byteLength |
Instance property | Returns the current size of the buffer in bytes |
.slice() |
Instance method | Returns a new SharedArrayBuffer holding a copy of a range of bytes |
.growable, .maxByteLength |
Instance property | Return whether the buffer can grow and the largest size it can reach (ES2024) |
.grow() |
Instance method | Grows a growable buffer in place, filling new bytes with zeros (ES2024) |
Examples
A log analyzer splits its work across four worker threads that all add to one shared hit counter. Workers come from the host, so this program uses the node:worker_threads module, and Atomics.add() keeps concurrent increments from overwriting each other:
count-hits.mjs
import { once } from "node:events";
import { Worker, isMainThread, workerData } from "node:worker_threads";
if (isMainThread) {
const shared = new SharedArrayBuffer(4);
const workers = [1, 2, 3, 4].map(
() => new Worker(new URL(import.meta.url), { workerData: shared }),
);
await Promise.all(workers.map((worker) => once(worker, "exit")));
console.log(`Total hits: ${new Int32Array(shared)[0]}`);
} else {
const hits = new Int32Array(workerData);
for (let i = 0; i < 100_000; i++) {
Atomics.add(hits, 0, 1);
}
}
The main thread reads the total that all four workers wrote into the same memory:
$ node count-hits.mjs
Total hits: 400000
An audio recorder shares a sample buffer with a processing thread but doesn’t know how long the recording will be. A growable buffer gets bigger in place, and a typed array created without a length tracks its current size:
> const recording = new SharedArrayBuffer(4, { maxByteLength: 16 });
undefined
> const samples = new Uint8Array(recording);
undefined
> samples.length
4
> recording.grow(8);
undefined
> samples.length
8
Common Gotchas
A growable SharedArrayBuffer can’t shrink. Asking .grow() for a smaller size throws a RangeError:
> const pool = new SharedArrayBuffer(8, { maxByteLength: 16 });
undefined
> pool.grow(4)
Uncaught RangeError: SharedArrayBuffer.prototype.grow: Invalid length parameter
at SharedArrayBuffer.grow (<anonymous>)
The .slice() method copies bytes into new memory, so the result isn’t shared with the original buffer:
> const original = new SharedArrayBuffer(4);
undefined
> const copy = original.slice();
undefined
> new Uint8Array(original)[0] = 9;
9
> new Uint8Array(copy)[0]
0
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 12, 2026