Atomics.compareExchange( ) In JavaScript

What is Atomics?

  • The Atomics is an object in JavaScript which provides the ability to perform atomic operations 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.

Atomics Operations in JavaScript
Multiple threads can read and write the same data in memory when there shared memory is. To ensure that predicted values are written and read accurately, another operation cannot start until and unless the current one finishes. Atomic operations also cannot be interrupted.

Atomics.compareExchange() Method



  • Among the Atomic Operations, there is an inbuilt method Atomics.compareExchange() which 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.
  • Atomics.compareExchange( ) operation in JavaScript exchanges the value in the array only if the passed parameter is equal to the old value at that position.
  • Atomics.compareExchange( ) operation returns the older value residing at that position of the array whether if its equal or not to the expected value.
  • No other write operation can happen until the modified value is written back.

Syntax:

Atomics.compareExchange(typedArray, index, expectedValue, replacementValue)

Parameters Used:

  • typedarray: It is the shared integer typed array you want to modify.
  • index: It is the position in the typedArray from where you want to exchange a value.
  • expectedValue : It is the number for which equality has to be checked.
  • replacementValue : It is the number to be exchanged.
  • Return Value:

  • Atomics.compareExchange() returns the old value at the given position (typedArray[index]).

Examples of the above function are provided below.

Examples:

Input : arr[0] = 9;
        Atomics.compareExchange(arr, 0, 9, 5);
Output : 5
Input : arr[0] = 3; 
        Atomics.compareExchange(arr, 0, 3, 6);
Output : 6

Codes for the above function are provided below.

Code 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// creating a SharedArrayBuffer 
var buf = new SharedArrayBuffer(25);
var arr = new Uint8Array(buf);
  
// Initialising element at zeroth position of array with 9
arr[0] = 9;
  
// Displaying the return value of the Atomics.compareExchange() method 
console.log(Atomics.compareExchange(arr, 0, 9, 5));
  
// Displaying the updated SharedArrayBuffer 
console.log(Atomics.load(arr, 0));
</script>

chevron_right


OUTPUT :

9
5

Code 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// creating a SharedArrayBuffer 
var buf = new SharedArrayBuffer(25);
var arr = new Uint8Array(buf);
  
// Initialising element at second position of array with 3
arr[2] = 3;
  
// Displaying the return value of the Atomics.compareExchange() method
console.log(Atomics.compareExchange(arr, 2, 3, 6));
  
// Displaying the updated SharedArrayBuffer 
console.log(Atomics.load(arr, 2);
</script>

chevron_right


OUTPUT :


3
6

    Application:

  • Whenever we want to exchange(only if the passed parameter equals the value) a value at a specific position of an array and also want to return the older value which was at that position of the array, we use the Atomics.compareExchange() operation in JavaScript.

    Let’s see a JavaScript Program :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
    // creating a SharedArrayBuffer 
    var mybuffer = new SharedArrayBuffer(25);
    var myarray = new Uint8Array(mybuffer);
      
    // Initialising the element at zeroth position of array
    myarray[0] = 40;
      
    // Displaying the return value of the Atomics.compareExchange() method 
    console.log(Atomics.compareExchange(myarray, 0, 40, 20));
      
    // Displaying the updated SharedArrayBuffer 
    console.log(Atomics.load(myarray, 0));
    </script>

    chevron_right

    
    

    Output :

    40
    20
    

Exceptions :

  1. If the typedArray is not one of the allowed integer types then the Atomics.compareExchange( ) operation throws a TypeError.
  2. If the typedArray is not a shared typed array then the Atomics.compareExchange( ) operation throws a TypeError.
  3. If the index used as an argument to the Atomics.compareExchange( ) operation is out of the bound in the typedArray then the Atomics.compareExchange( ) operation throws a RangeError.

Difference between Atomics.compareExchange and Atomics.exchange
As we read that the Atomics.compareExchange() operation in JavaScript exchanges the value in the array only if the passed parameter is equal to the old value at that position whereas the Atomics.exchange() operation in JavaScript exchanges and stores a given value at a given position in the array without comparing it with the older value residing at that position of the array.
Both the operations return the older value which was present at the specified position.



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.