Skip to content
Related Articles

Related Articles

Improve Article

Atomics.isLockFree( ) In JavaScript

  • Last Updated : 02 Aug, 2021
Geek Week

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.isLockFree() Method 
 

  • Atomics.isLockFree() operation returns true if the given size is one of the BYTES_PER_ELEMENT property of integer TypedArray types else Atomics.isLockFree() operation returns false.
  • A lock free element can be manipulated without needing a lock and the user does not require to provide its own locking mechanism.

What is BYTES_PER_ELEMENT property of integer TypedArray ? 
 

  • The TypedArray.BYTES_PER_ELEMENT property represents the size in bytes of each element in a typed array.
  • Since TypedArray objects differ from each other in the number of bytes per element and in the way the bytes are interpreted.
  • The BYTES_PER_ELEMENT constant contains the number of bytes each element in the given TypedArray has.

Applications: 
 

  • Atomics.isLockFree() are used to check whether an operation is lock free or not.
  • Can be used for the validation of the BYTES_PER_ELEMENT property of integer TypedArray.

Syntax: 
 



Atomics.isLockFree(size)

 

Parameters Used: 
size : It is the size in bytes to check
Return Value: 
Atomics.isLockFree() returns either Boolean true indicating the operation is lock free or it returns false. 
 

Examples of the above function are provided below.
 

Input : Atomics.isLockFree(5)
Output : false

Explanation: In this example, “5” is sent as a parameter to the Atomics.isLockFree() method and it returns false because “5” is not one of the BYTES_PER_ELEMENT values. 
 

Input : Atomics.isLockFree(6)
Output : false

Explanation: In this example, “6” is sent as a parameter to the Atomics.isLockFree() method and it returns false because “6” is not one of the BYTES_PER_ELEMENT values. 
 

Input : Atomics.isLockFree(2)
Output : true

Explanation: In this example, “2” is sent as a parameter to the Atomics.isLockFree() method and it returns true because “2” is one of the BYTES_PER_ELEMENT values. 
 

Input : Atomics.isLockFree(4)
Output : true

Explanation: In this example, “4” is sent as a parameter to the Atomics.isLockFree() method and it returns true because “4” is one of the BYTES_PER_ELEMENT values. 
Codes for the above function are provided below.
Code 1: 
 

javascript




<script>
// Displaying the return value of the
// Atomics.isLockFree() method
console.log(Atomics.isLockFree(5));
 
// Atomics.isLockFree() will return false since
// 5 is not one of the BYTES_PER_ELEMENT values
</script>

OUTPUT : 
 



false

Code 2:
 

javascript




<script>
// Displaying the return value of
// the Atomics.isLockFree() method
console.log(Atomics.isLockFree(6));
 
// Atomics.isLockFree() will return false since 6
// is not one of the BYTES_PER_ELEMENT values
</script>

OUTPUT : 
 

false

Code 3:
 

javascript




<script>
// Displaying the return value
// of the Atomics.isLockFree() method
console.log(Atomics.isLockFree(2));
 
// Atomics.isLockFree() will return true since
// 2 is one of the BYTES_PER_ELEMENT values
</script>

OUTPUT : 
 

true

Code 4:
 

javascript




<script>
// Displaying the return value of the
// Atomics.isLockFree() method
console.log(Atomics.isLockFree(4));
 
// Atomics.isLockFree() will return true since
//  4 is one of the BYTES_PER_ELEMENT values
</script>

OUTPUT : 
 

true

Application: 
Whenever we want to check whether an operation is lock-free or not or we want to validate the BYTES_PER_ELEMENT property of integer TypedArray, we use the Atomics.isLockFree() operation in JavaScript.
Let’s see a JavaScript Program : 
 

javascript




<script>
// Displaying the return value of
// the Atomics.isLockFree() method
console.log(Atomics.isLockFree(8));
 
// Atomics.isLockFree() will return true since 8
// is one of the BYTES_PER_ELEMENT(Float64Array) values
</script>

Output : 
 

true

Exceptions :
 

  • If the typedArray is not one of the allowed integer types then the Atomics.isLockFree( ) operation throws a TypeError.
  • If the typedArray is not a shared typed array then the Atomics.isLockFree( ) operation throws a TypeError.

Supported Browser:

  • Google Chrome
  • Microsoft Edge
  • Firefox

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!




My Personal Notes arrow_drop_up
Recommended Articles
Page :