bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/Objects, Classes, and Advanced Patterns
JavaScript•Objects, Classes, and Advanced Patterns

JavaScript Atomics

The Atomics Object

The Atomics Object provides low-level atomic operations on shared memory.

It is used with SharedArrayBuffer and Typed Arrays to share data between workers.

What Are Atomics?

  • Work on shared typed arrays
  • Are performed atomically (cannot be interrupted halfway)
  • Return the previous value of the element

Atomics are an advanced feature.

You typically use Atomics when you work with:

  • Web Workers
  • Worker Threads
  • Shared memory

Requirements

To use Atomics you need

  • A SharedArrayBuffer
  • A typed array that uses the shared buffer (e.g. Int32Array )
  • One or more workers (or threads) that share the same buffer

Basic Atomics Usage

Example: Creating a Shared Typed Array

const buffer = new SharedArrayBuffer(4 * Int32Array.BYTES_PER_ELEMENT);
// Shared integer array with 4 elements
const sharedArray = new Int32Array(buffer);
// Initialize the array sharedArray[0] = 10; sharedArray[1] = 20; sharedArray[2] = 30; sharedArray[3] = 40;

Atomic Read and Write

Use Atomics.load() to read and Atomics.store() to write an element in a shared typed array .

Example: Atomics.load() and Atomics.store()

var buffer = new SharedArrayBuffer(4 * Int32Array.BYTES_PER_ELEMENT);
var sharedArray = new Int32Array(buffer);
// Store a value atomically
Atomics.store(sharedArray, 0, 123);
// Load the value atomically
var value = Atomics.load(sharedArray, 0);
console.log("Value:", value); // Value: 123

Atomics.store() returns the value you stored.

Atomics.load() returns the current value of the element.

Atomic Add and Subtract

Atomics.add() and Atomics.sub() change a value and return the old value.

Example: Atomics.add()

var buffer = new SharedArrayBuffer(4 * Int32Array.BYTES_PER_ELEMENT);
var sharedArray = new Int32Array(buffer);
sharedArray[0] = 5;
var oldValue = Atomics.add(sharedArray, 0, 3);
console.log("Old:", oldValue); // Old: 5
console.log("New:", sharedArray[0]);// New: 8

Example: Atomics.sub()

var buffer = new SharedArrayBuffer(4 * Int32Array.BYTES_PER_ELEMENT);
var sharedArray = new Int32Array(buffer);
sharedArray[0] = 10;
var oldValue = Atomics.sub(sharedArray, 0, 4);
console.log("Old:", oldValue); // Old: 10
console.log("New:", sharedArray[0]);// New: 6

Atomic Exchange and CompareExchange

Atomics.exchange() sets a new value and returns the old one. Atomics.compareExchange() only sets a new value if the current value is equal to a given expected value.

Example: Atomics.exchange()

var buffer = new SharedArrayBuffer(4 * Int32Array.BYTES_PER_ELEMENT);
var sharedArray = new Int32Array(buffer);
sharedArray[0] = 1;
var oldValue = Atomics.exchange(sharedArray, 0, 99);
console.log("Old:", oldValue); // Old: 1
console.log("New:", sharedArray[0]); // New: 99

Example: Atomics.compareExchange()

var buffer = new SharedArrayBuffer(4 * Int32Array.BYTES_PER_ELEMENT);
var sharedArray = new Int32Array(buffer);
sharedArray[0] = 10;
// Only replace 10 with 20 if the current value is 10
var oldValue = Atomics.compareExchange(sharedArray, 0, 10, 20);
console.log("Old:", oldValue); // Old: 10
console.log("New:", sharedArray[0]); // New: 20
// Now it will NOT change because the expected value does not match
oldValue = Atomics.compareExchange(sharedArray, 0, 10, 30);
console.log("Old:", oldValue); // Old: 20
console.log("New:", sharedArray[0]); // New: 20

Atomics.wait() and Atomics.notify()

Atomics.wait() (in workers) can put a thread to sleep until the value at a position changes, and Atomics.notify() wakes up one or more sleeping threads. Important: Atomics.wait() can only be used in worker contexts (not on the main thread) in browsers. Example: Worker Waiting for a Value Main thread (simplified): // main.js var buffer = new SharedArrayBuffer(4); var sharedArray = new Int32Array(buffer); sharedArray[0] = 0; var worker = new Worker("worker.js"); // Send the shared buffer to the worker worker.postMessage(buffer); // Simulate some work, then set the value and notify the worker setTimeout(function() { Atomics.store(sharedArray, 0, 1); Atomics.notify(sharedArray, 0, 1); // wake 1 worker }, 1000); Worker (worker.js): // worker.js onmessage = function(e) { var buffer = e.data; var sharedArray = new Int32Array(buffer); console.log("Worker waiting..."); var result = Atomics.wait(sharedArray, 0, 0); // wait while value is 0 console.log("Worker woken, result =", result); console.log("New value:", Atomics.load(sharedArray, 0)); }; Complete Atomics Reference Revised December 2025 Method Description load(typedArray, index) Reads and returns the value at index. store(typedArray, index, value) Stores value at index and returns it. add(typedArray, index, value) Adds value to the element and returns the old value. sub(typedArray, index, value) Subtracts value and returns the old value. and(typedArray, index, value) Performs bitwise AND with value and returns the old value. or(typedArray, index, value) Performs bitwise OR with value and returns the old value. xor(typedArray, index, value) Performs bitwise XOR with value and returns the old value. exchange(typedArray, index, value) Replaces the value at index and returns the old value. compareExchange(typedArray, index, expected, value) Replaces the value at index with value if the current value is expected. Returns the old value. wait(typedArray, index, value, timeout?) (Worker only) Blocks the calling thread until the element changes or timeout. notify(typedArray, index, count) Wakes up sleeping threads that are waiting on index. Atomics are for communication between threads using shared memory You use them with SharedArrayBuffer and Typed Arrays Atomics operations can not be interrupted by other threads Use Atomics only when you need low-level synchronization Most JavaScript code does not need Atomics Learn More: Typed Array Tutorial Typed Array Methods Typed Array Reference Array Buffers DataViews

Important: Atomics.wait() can only be used in worker contexts (not on the main thread) in browsers. Example: Worker Waiting for a Value Main thread (simplified): // main.js var buffer = new SharedArrayBuffer(4); var sharedArray = new Int32Array(buffer); sharedArray[0] = 0; var worker = new Worker("worker.js"); // Send the shared buffer to the worker worker.postMessage(buffer); // Simulate some work, then set the value and notify the worker setTimeout(function() { Atomics.store(sharedArray, 0, 1); Atomics.notify(sharedArray, 0, 1); // wake 1 worker }, 1000); Worker (worker.js): // worker.js onmessage = function(e) { var buffer = e.data; var sharedArray = new Int32Array(buffer); console.log("Worker waiting..."); var result = Atomics.wait(sharedArray, 0, 0); // wait while value is 0 console.log("Worker woken, result =", result); console.log("New value:", Atomics.load(sharedArray, 0)); }; Complete Atomics Reference Revised December 2025 Method Description load(typedArray, index) Reads and returns the value at index. store(typedArray, index, value) Stores value at index and returns it. add(typedArray, index, value) Adds value to the element and returns the old value. sub(typedArray, index, value) Subtracts value and returns the old value. and(typedArray, index, value) Performs bitwise AND with value and returns the old value. or(typedArray, index, value) Performs bitwise OR with value and returns the old value. xor(typedArray, index, value) Performs bitwise XOR with value and returns the old value. exchange(typedArray, index, value) Replaces the value at index and returns the old value. compareExchange(typedArray, index, expected, value) Replaces the value at index with value if the current value is expected. Returns the old value. wait(typedArray, index, value, timeout?) (Worker only) Blocks the calling thread until the element changes or timeout. notify(typedArray, index, count) Wakes up sleeping threads that are waiting on index. Atomics are for communication between threads using shared memory You use them with SharedArrayBuffer and Typed Arrays Atomics operations can not be interrupted by other threads Use Atomics only when you need low-level synchronization Most JavaScript code does not need Atomics Learn More: Typed Array Tutorial Typed Array Methods Typed Array Reference Array Buffers DataViews

Example: Worker Waiting for a Value

Main thread (simplified)

// main.js
var buffer = new SharedArrayBuffer(4);
var sharedArray = new Int32Array(buffer);
sharedArray[0] = 0;
var worker = new Worker("worker.js");
// Send the shared buffer to the worker
worker.postMessage(buffer);
// Simulate some work, then set the value and notify the worker
setTimeout(function() {
 Atomics.store(sharedArray, 0, 1);
 Atomics.notify(sharedArray, 0, 1); // wake 1 worker
}, 1000);

Worker (worker.js):

// worker.js
onmessage = function(e) {
 var buffer = e.data;
 var sharedArray = new Int32Array(buffer);
 console.log("Worker waiting...");
 var result = Atomics.wait(sharedArray, 0, 0); // wait while value is 0
 console.log("Worker woken, result =", result);
 console.log("New value:", Atomics.load(sharedArray, 0));
};

Learn More

Typed Array Methods

Typed Array Reference

Array Buffers

DataViews

Previous

JavaScript DataView

Next

JavaScript HTML DOM Navigation

This chapter

Overview
123

Lessons

539m

Read time

1. JavaScript Objects2. JavaScript Temporal Tutorial3. JavaScript Loops4. JavaScript Advanced Functions5. JavaScript Objects6. JavaScript Temporal Intro7. JavaScript Iterables8. JavaScript Function Definitions9. JavaScript Object Properties10. JavaScript Temporal vs Date11. JavaScript Iterators12. JavaScript Callbacks13. JavaScript Object Methods14. JavaScript Temporal Duration15. JavaScript Generators16. The JavaScript this Keyword17. JavaScript this Keyword18. JavaScript Temporal Instant19. JavaScript Function call()20. JavaScript Display Objects21. JavaScript PlainDateTime22. JavaScript Function apply()23. JavaScript Object Constructors24. JavaScript Temporal PlainDate25. JavaScript Function bind()26. JavaScript PlainYearMonth27. Self-Invoking Functions28. JavaScript PlainMonthDay29. JavaScript Closures30. JavaScript Temporal PlainTime31. JavaScript Objects - Advanced32. JavaScript ZonedDateTime33. JavaScript Object Definitions34. JavaScript Temporal Now35. this in JavaScript Objects36. JS Temporal Arithmetic37. JavaScript Object Iterations38. JS Temporal Differences39. JavaScript Object Accessors40. JS Temporal Compare41. JavaScript Object Management42. JavaScript Temporal Conversion43. JavaScript Object Protection44. Temporal Display Formats45. JavaScript Object Prototypes46. JavaScript Temporal Mistakes47. JavaScript Classes48. Migrate from Date to Temporal49. JavaScript Class Inheritance50. Temporal Standards51. JavaScript Static Methods52. Asynchronous JavaScript53. Asynchronous Programming54. JavaScript Timeouts55. JavaScript Callbacks56. JavaScript Promises57. JavaScript async and await58. JavaScript fetch API59. Debugging Async JavaScript60. JavaScript Modules61. JavaScript Modules Export62. JavaScript Modules Import63. JS Module Namespace64. JavaScript Dynamic Modules65. JavaScript Meta Programming66. JavaScript Reflect67. JavaScript Proxy68. JavaScript Typed Arrays69. Typed Array Methods70. JavaScript ArrayBuffer71. JavaScript DataView72. JavaScript Atomics73. JavaScript HTML DOM Navigation74. JavaScript HTML DOM Elements (Nodes)75. JavaScript HTML DOM Collections76. JavaScript HTML DOM Node Lists77. JavaScript Window - The Browser Object Model78. JavaScript Window Screen79. JavaScript Window Location80. JavaScript Window History81. JavaScript Window Navigator82. JavaScript Popup Boxes83. JavaScript Timing Events84. JavaScript Cookies85. Web APIs - Introduction86. JavaScript Fetch API87. Web Geolocation API88. Web History API89. Pointer Events API90. Web Storage API91. JavaScript Validation API92. Web Workers API93. AJAX Introduction94. AJAX - The XMLHttpRequest Object95. AJAX - XMLHttpRequest96. AJAX - Server Response97. AJAX XML Example98. AJAX PHP Example99. AJAX ASP Example100. AJAX Database Example101. XML Applications102. JavaScript JSON103. JSON Syntax104. JSON vs XML105. JSON Data Types106. JSON.parse()107. JSON.stringify()108. JSON Object Literals109. JSON Array Literals110. JSON PHP111. JSON HTML112. JSONP113. JavaScript / jQuery DOM Selectors114. JavaScript / jQuery HTML Elements115. JavaScript / jQuery CSS Styles116. JavaScript / jQuery HTML DOM117. JavaScript Graphics118. HTML Canvas119. Plotly.js120. Chart.js121. Google Chart122. D3.js123. JavaScript Interview Preparation

On this page

The Atomics ObjectWhat Are Atomics?RequirementsBasic Atomics UsageAtomic Read and WriteExample: Atomics.load() and Atomics.store()Atomic Add and SubtractExample: Atomics.add()Example: Atomics.sub()Atomic Exchange and CompareExchangeExample: Atomics.exchange()Example: Atomics.compareExchange()Atomics.wait() and Atomics.notify()Example: Worker Waiting for a Value