Atomics
The Atomics global object is a namespace that groups operations for reading and writing shared memory safely from several threads at once. It can’t be constructed and only holds static members. Its read and write operations each work on one element of an integer typed array and finish as a single indivisible step, so no other thread sees them half done:
> const hits = new Int32Array(new SharedArrayBuffer(8));
undefined
> Atomics.add(hits, 0, 5)
0
> Atomics.load(hits, 0)
5
Read-modify-write operations such as Atomics.add() return the value the element held before the change. That old value tells a thread exactly what it overwrote, even when other threads are writing to the same element.
Atomics is mostly used with a SharedArrayBuffer, whose memory several workers can read and write at the same time. Plain reads and writes such as hits[0] += 1 aren’t atomic there, so two threads can overwrite each other’s update. The atomic operations also accept typed arrays over a regular ArrayBuffer, apart from the waiting methods.
The widget below steps two workers through adding 1 to the same element, once with a plain hits[0] += 1 and once with an atomic add:
Like Math, Atomics isn’t a constructor, so using it with new or calling it throws a TypeError. Atomics was added in ES2017 along with SharedArrayBuffer.
Key Static Methods and Properties
| Member | Kind | Description |
|---|---|---|
Atomics.load() |
Static method | Returns the value at an index |
Atomics.store() |
Static method | Writes a value at an index and returns the value written |
Atomics.add(), Atomics.sub() |
Static method | Add or subtract a value at an index and return the old value |
Atomics.and(), Atomics.or(), Atomics.xor() |
Static method | Apply a bitwise operation at an index and return the old value |
Atomics.exchange() |
Static method | Replaces the value at an index and returns the old value |
Atomics.compareExchange() |
Static method | Replaces the value at an index only when it equals an expected value, and returns the old value |
Atomics.wait() |
Static method | Blocks the thread until notified or timed out, and returns "ok", "not-equal", or "timed-out" |
Atomics.waitAsync() |
Static method | Waits without blocking and returns an object whose value may be a promise (ES2024) |
Atomics.notify() |
Static method | Wakes threads waiting on an index and returns how many it woke |
Atomics.isLockFree() |
Static method | Returns whether operations on elements of a given byte size run without a lock |
Atomics.pause() |
Static method | Hints to the CPU that the thread is spinning in a wait loop, and returns undefined. Expected in ES2027 |
Every method except Atomics.isLockFree(), which takes a byte size, and Atomics.pause() takes a typed array and an index as its first two arguments. Atomics.waitAsync() is Baseline Newly available since 2025. Atomics.pause() reached Stage 4 in May 2026 and is expected in ES2027. Browsers shipped it earlier, and it’s Baseline Newly available since 2025.
Examples
Several workers pull jobs from a shared table, and each job must run exactly once. Atomics.compareExchange() claims a job by switching its slot from 0 to 1, and only the thread that sees 0 come back wins:
> const claimed = new Int32Array(new SharedArrayBuffer(16));
undefined
> Atomics.compareExchange(claimed, 2, 0, 1)
0
> Atomics.compareExchange(claimed, 2, 0, 1)
1
The second call finds 1 already stored, so it changes nothing, and that thread moves on to another job.
A main thread hands a slow calculation to a worker and needs the result before it continues. Workers come from the host, so this program uses the node:worker_threads module. The main thread sleeps in Atomics.wait() until the worker stores the result and wakes it with a call to Atomics.notify() on the same index:
handoff.mjs
import { Worker, isMainThread, workerData } from "node:worker_threads";
if (isMainThread) {
const shared = new Int32Array(new SharedArrayBuffer(8));
new Worker(new URL(import.meta.url), { workerData: shared });
Atomics.wait(shared, 0, 0);
console.log(`Total: ${Atomics.load(shared, 1)}`);
} else {
let total = 0;
for (let n = 1; n <= 1000; n++) {
total += n;
}
Atomics.store(workerData, 1, total);
Atomics.store(workerData, 0, 1);
Atomics.notify(workerData, 0);
}
Running the file prints the worker’s total after the main thread wakes up:
$ node handoff.mjs
Total: 500500
Index 0 is a ready flag. If the worker sets it before the main thread starts waiting, Atomics.wait() sees a value other than 0 and returns "not-equal" right away instead of sleeping forever.
Common Gotchas
The atomic operations accept only integer typed arrays. Float arrays and Uint8ClampedArray throw a TypeError:
> const prices = new Float64Array(new SharedArrayBuffer(16));
undefined
> Atomics.add(prices, 0, 1)
Uncaught TypeError: [object Float64Array] is not an integer typed array.
at Atomics.add (<anonymous>)
Atomics.wait() and Atomics.waitAsync() need an Int32Array or BigInt64Array that views a SharedArrayBuffer. A typed array over a regular ArrayBuffer throws, and Atomics.notify() on one returns 0:
> const flags = new Int32Array(4);
undefined
> Atomics.wait(flags, 0, 0)
Uncaught TypeError: [object Int32Array] is not a shared typed array.
at Atomics.wait (<anonymous>)
> Atomics.notify(flags, 0)
0
Whether a thread may block depends on the host. Browsers throw a TypeError when the main thread calls Atomics.wait(), so pages call it in a worker or use Atomics.waitAsync() instead. Node.js lets its main thread block, which is why the handoff.mjs example works.
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