Skip to main content

What is SharedArrayBuffer?

Definition

SharedArrayBuffer is a special object type in JavaScript that provides a shared memory region, accessible to several threads at once (for example, worker_threads in 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

javascript
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

javascript
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

PropertyArrayBufferSharedArrayBuffer
Copied on transferYesNo
Shared access between threadsNoYes
Atomics supportNoYes
Safe under concurrent writesNot applicableOnly with Atomics
Typical purposeA buffer for one threadShared 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:

javascript
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)); // 7

4. 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

AdvantageDrawback
Fast data transfer with no copyingDangerous if synchronized incorrectly
Memory savingsNo protection against concurrent writes
Atomic operations availableHarder to debug and reason about
Supported in browsers and Node.jsPlain objects can't be passed this way

6. A practical example: a counter shared across threads

main.js

javascript
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

javascript
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

CharacteristicDescription
PurposeShared memory between threads
TypeA binary memory object
Compatible withTypedArray (Int8Array, Float64Array, etc.)
Safe useOnly with Atomics
Where it's usedNode.js (worker_threads), browsers (Web Workers)
AdvantagesFast, no copying, efficient
DrawbacksMore complex logic, risk of data races

Conclusion:

SharedArrayBuffer is a "shared piece of memory" that several threads can read and change at once. In Node.js it's used together with worker_threads and Atomics, when you need to exchange data fast, without copying, and with controlled synchronization.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.