Skip to content
Related Articles

Related Articles

JavaScript Atomics Complete Reference

Improve Article
Save Article
  • Last Updated : 15 Nov, 2022
Improve Article
Save Article

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:

Atomics.method(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




<script>
    // creating a SharedArrayBuffer
    var buf = new SharedArrayBuffer(25);
    var 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));
</script>

Output:

9

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

JavaScript Atomics Methods

Descriptions

JavaScript Atomics.add( ) MethodThis is used to add a given value at a given position in an array and return the old value at that position.
JavaScript Atomics.and() MethodThis is used to compute a bitwise AND with a given value at a given position in the array.
JavaScript Atomics.compareExchange( ) MethodThis is used to exchange a value at a specific position of an array if the passed parameter is equal to the old value residing in the array.
JavaScript Atomics.exchange( ) MethodThis is used to exchange and store a new value at a specific position in an array.
JavaScript Atomics.isLockFree( ) MethodIf returns true if the given size is one of the BYTES_PER_ELEMENT properties of integer.
JavaScript Atomics.load( ) MethodJavaScript Atomics.load() returns the value at the given position of the array.
JavaScript Atomics.notify() MethodThis is used to notify up some agents that are sleeping in the wait queue.
JavaScript Atomics.or( ) MethodThis is used to compute a bitwise OR operation with a given value at a given position in an array.
JavaScript Atomics.store( ) MethodAtomics.store() operation returns the value which was has been stored.
JavaScript Atomics.sub( ) MethodThis is used to subtract a given value at a given position in the array and returns the old value at that position.
JavaScript Atomics.wait() MethodThe Atomics.wait() method returns a string that is either “ok”, “not-equal”, or “timed-out”.
JavaScript Atomics.xor( ) MethodThis is used to compute a bitwise XOR operation with a given value at a given position in an array.
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!