Open In App

JavaScript Atomics Reference

Last Updated : 30 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Atomics is an object in JavaScript that provides atomic operations to be performed as static methods Just like the Math object in JavaScript, all the properties and methods of Atomics are also static Atomics are used with SharedArrayBuffer (generic fixed-length binary data buffer) objects Atomics are not constructors like other global objects Atomics cannot be used with a new operator or can be invoked as a function

Syntax:

Atomicsmethod(argument 1, argument 2, )

Example: In this example load() function of the Atomic object returns the value at a given position of an array

Javascript




// creating a SharedArrayBuffer
let buf = new SharedArrayBuffer(25);
let arr = new Uint8Array(buf);
 
// Initialising element at the zeroth
//position of an array with 9
arr[0] = 9;
 
// Displaying the SharedArrayBuffer
console.log(Atomics.load(arr, 0));


Output:

9

JavaScript Atomics Methods: The complete list of JavaScript Atomics methods are listed below

 Methods

Descriptions

Examples

add( ) Given the position in an array and return the old value at that position
and( ) Compute a bitwise AND with a given value at a given position in the array
compareExchange( )  It compares and exchanges a replacement value
exchange( ) This is used to exchange and store a new value at a specific position in an array
is lock-free( ) Returns true if the given size is one of the BYTES_PER_ELEMENT properties of integer
load( ) Returns the value at the given position of the array
notify() Notify some agents that are sleeping in the wait queue
or( ) Compute a bitwise OR operation with a given value at a given position in an array
store( ) Returns the value which was has been stored
sub( ) Returns the old value at that position
wait() Returns a string that is either “ok”, “not-equal”, or “timed-out”
waitasync() Returns a promise without blocking the main thread
xor( ) Compute a bitwise XOR operation with a given value at a given position in an array

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads