What is SharedArrayBuffer?
Definition
SharedArrayBufferis a special object type in JavaScript that provides a shared memory region, accessible to several threads at once (for example,worker_threadsin Node.js).
The idea
Ordinary objects and arrays get copied when passed between threads.
SharedArrayBuffer lets you:
- share the exact same memory region between threads,
- avoid copying large data,
- synchronize access through atomic operations (
Atomics).
In other words:
It's like a "shared chunk of RAM" that every thread inside one Node.js process can read and change.
1. An example
main.js
const { Worker } = require('worker_threads');
// Create a shared buffer of 4 bytes * 10 elements
const shared = new SharedArrayBuffer(4 * 10);
const sharedArray = new Int32Array(shared);
sharedArray[0] = 10;
sharedArray[1] = 20;
const worker = new Worker('./worker.js', { workerData: shared });
worker.on('exit', () => {
console.log('The main thread sees:', sharedArray[0], sharedArray[1]);
// for example, 20 40
});worker.js
const { workerData } = require('worker_threads');
// workerData holds a reference to the same SharedArrayBuffer
const arr = new Int32Array(workerData);
// Modify the values in the shared buffer
arr[0] *= 2;
arr[1] *= 2;Here, main.js and worker.js work with the exact same memory region.
Changes made in the thread are visible immediately in the main thread.
2. The difference from ArrayBuffer
| Property | ArrayBuffer | SharedArrayBuffer |
|---|---|---|
| Copied on transfer | Yes | No |
| Shared access between threads | No | Yes |
Atomics support | No | Yes |
| Safe under concurrent writes | Not applicable | Only with Atomics |
| Typical purpose | A buffer for one thread | Shared memory between threads |
3. Why Atomics is needed
When several threads simultaneously read and write shared data, a race condition can happen.
To avoid it, the Atomics object performs operations (reading, writing, incrementing, and so on) atomically,
guaranteeing one operation completes fully before another starts.
Example:
const shared = new SharedArrayBuffer(4);
const arr = new Int32Array(shared);
Atomics.store(arr, 0, 5);
Atomics.add(arr, 0, 2);
console.log(Atomics.load(arr, 0)); // 74. Where SharedArrayBuffer is used
Useful when:
- you need to exchange large binary data without copying;
- parallel computation is happening (rendering, simulations, ML, parsing);
- you need fast exchange between threads (
worker_threads); - you need shared state synchronized with
Atomics.
Not needed when:
- message passing (
postMessage) is enough; - the data is simple (strings, JSON, objects);
- safety matters more than performance.
5. Advantages and drawbacks
| Advantage | Drawback |
|---|---|
| Fast data transfer with no copying | Dangerous if synchronized incorrectly |
| Memory savings | No protection against concurrent writes |
| Atomic operations available | Harder to debug and reason about |
| Supported in browsers and Node.js | Plain objects can't be passed this way |
6. A practical example: a counter shared across threads
main.js
const { Worker } = require('worker_threads');
const shared = new SharedArrayBuffer(4);
const counter = new Int32Array(shared);
counter[0] = 0;
const worker1 = new Worker('./worker.js', { workerData: shared });
const worker2 = new Worker('./worker.js', { workerData: shared });
worker1.on('exit', () => worker2.on('exit', () => {
console.log('Result:', counter[0]); // 2000000
}));worker.js
const { workerData } = require('worker_threads');
const counter = new Int32Array(workerData);
for (let i = 0; i < 1_000_000; i++) {
Atomics.add(counter, 0, 1);
}Both threads safely increment the exact same counter,
with no data races, thanks to Atomics.
Summary
| Characteristic | Description |
|---|---|
| Purpose | Shared memory between threads |
| Type | A binary memory object |
| Compatible with | TypedArray (Int8Array, Float64Array, etc.) |
| Safe use | Only with Atomics |
| Where it's used | Node.js (worker_threads), browsers (Web Workers) |
| Advantages | Fast, no copying, efficient |
| Drawbacks | More complex logic, risk of data races |
Conclusion:
SharedArrayBufferis a "shared piece of memory" that several threads can read and change at once. In Node.js it's used together withworker_threadsandAtomics, when you need to exchange data fast, without copying, and with controlled synchronization.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.