Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Atomics in JavaScript

  • Difficulty Level : Hard
  • Last Updated : 18 Mar, 2021

Atomics: Atomics is a JavaScript object which gives atomic tasks to proceed as static strategies. Much the same as the strategies for Math object, the techniques, and properties of Atomics are additionally static. Atomics are utilized with SharedArrayBuffer objects. The Atomic activities are introduced on an Atomics module. In contrast to other worldwide articles, Atomics isn’t a constructor. Atomics can’t be utilized with another administrator or can be summoned as a capacity.

Atomic Operations:  Atomic operations are not continuous. Multiple threads can read and write data in the memory when memory is shared. There is a loss of data if any data has changed Atomic operations ensure the data is written and accurately read by the predicted values. There is no way to change existing information until the current operation is completed and atomic operations will start.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Methods: 

  • Atomics.add(): Adds the value provided to the current value in the array index specified. Returns the old index value.
  • Atomics.and(): The value AND is computed bitwise on the index of the array specified with the value provided. Returns that index’s old value.
  • Atomics.exchange(): Specifies a value at the array index specified. The old value is returned.
  • Atomics.compareExchange(): Specifies the value in the specified array index if the value is the same. Old value returns.
  • Atomics.isLockFree(size): Primitive optimization to determine whether locks or atomic operations are to be used. Returns true if a hardware atomic operation is carried out in the arrays of the given element size (as opposed to a lock).
  • Atomics.load(): The value returns to the array index specified.
  • Atomics.or(): Bitwise OR computes the value with the given value at the specified array index. Returns the old index value.
  • Atomics.notify(): Notify agents waiting for the specified array index. Returns the notified number of agents.
  • Atomics.sub(): Deletes a value at the array index specified. Returns the old index value.
  • Atomics.store(): Save a value on the array index specified. Returns value.
  • Atomics.wait(): Verifies that the specified array index still has a value and waiting or waiting times are sleeping. Returns “ok,” “not the same,” or “time-out.” If the calling agent is unable to wait, it throws an exception to an error.
  • Atomics.xor(): Compute a bitwise XOR with the given value on the given array index. Returns the old index value.

Example 1:



Javascript




var buffer = new 
// create a SharedArrayBuffer
SharedArrayBuffer(50); 
var a = new Uint8Array(buffer);
// Initialising element at zeroth position of array with 9 
a[0] = 9;
console.log(Atomics.load(a, 0)); 
// Displaying the return value of the Atomics.store() method
console.log(Atomics.store(a, 0, 3)); 
// Displaying the updated SharedArrayBuffer 
console.log(Atomics.load(a, 0));

Output:

933

Example 2:

Javascript




const buffer = new SharedArrayBuffer(2048);
  
const ta = new Uint8Array(buffer);
  
ta[0]; // 0
  
ta[0] = 5; // 5
  
Atomics.add(ta, 0, 12);   // 5
  
Atomics.load(ta, 0);      // 17
  
Atomics.and(ta, 0, 1); // 17
  
Atomics.load(ta, 0); // 1
  
Atomics.exchange(ta, 0, 12); // 1
  
Atomics.load(ta, 0); // 12
  
Atomics.compareExchange(ta, 0, 5, 12); // 1
  
Atomics.load(ta, 0); // 1
  
Atomics.isLockFree(1); // true
  
Atomics.isLockFree(2); // true
  
Atomics.or(ta, 0, 1); // 12
  
Atomics.load(ta, 0);  // 13
  
Atomics.store(ta, 0, 12); // 12
  
Atomics.sub(ta, 0, 2); // 12
  
Atomics.load(ta, 0); // 10
  
Atomics.xor(ta, 0, 1); // 10
  
Atomics.load(ta, 0); // 11

Output:

5
17
17
1
1
12
1
1
True
True
13
13
12
12
10
10



My Personal Notes arrow_drop_up
Recommended Articles
Page :