Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the Atomics object do in JavaScript?

This is the MDN documentation on Atomics.

I can't understand how an atomic object could be used in a real-life situation or if they are for internal use only (i.e. not meant to be called at all).

What does the Atomics object do in JavaScript? How can I use them?

like image 230
adelriosantiago Avatar asked Jan 19 '18 17:01

adelriosantiago


People also ask

What are Atomics JavaScript?

The Atomics is an object in JavaScript which provides atomic operations to be performed as static methods. Just like the methods of Math object, the methods and properties of Atomics are also static. Atomics are used with SharedArrayBuffer objects. The Atromic operations are installed on an Atomics Module.

What is object Atomic?

By an "atomic object" we understand an object whose public interface exposes only atomic operations, i.e. all operations you can do with that object are atomic.

What does Atomic mean in C?

Atomicity. In computer programming, an operation done by a computer is considered atomic if it is guaranteed to be isolated from other operations that may be happening at the same time.

What are atomic operations in OS?

Atomic operations are sequences of instructions that guarantee atomic accesses and updates of shared single word variables. This means that atomic operations cannot protect accesses to complex data structures in the way that locks can, but they provide a very efficient way of serializing access to a single word.


2 Answers

This feature was introduced with ECMAScript 2017. Shared Memory and Atomics introduce a new memory model that allows multi-agent programs to communicate using atomic operations that ensure a well-defined execution order even on parallel CPUs. This specification also includes new static methods on Object: Object.values, Object.entries, and Object.getOwnPropertyDescriptors.

The main idea is to bring some sort of multi-threading feature to JavaScript so that JS developers can write high-performance, concurrent programs in the future by allowing to manage memory by themselves instead of letting JS engine manage memory.

This is done by a new type of a global object called SharedArrayBuffer that essentially stores data in a shared memory space. So this data can be shared between the main JS thread and web-worker threads.

You simply use SharedArrayBuffer and the data is instantly accessible by both the main thread and multiple web-worker threads.

But sharing memory between threads can cause race conditions. To help avoid race conditions, the Atomics global object is introduced. Atomics provides various methods to lock the shared memory when a thread is using its data. It also provides methods to update such data in that shared memory safely.

Let's wait some new abstract library which will use those API's right.

shared memory and atomics

a cartoon intro to SharedArrayBuffers

JavaScript: From Workers to Shared Memory

ECMAScript 2019. Memory Model

Avoiding race conditions in SharedArrayBuffers with Atomics

Also recommend to see the tutorial about shared memory in ECMAScript.

like image 52
BSeitkazin Avatar answered Oct 14 '22 01:10

BSeitkazin


Dangers of multithreaded programming

  • Deadlocks
  • Unpredictable
    order of read-write operations
  • Data fragmentation

To solve the previous problem what can we do?

Use the atomic option

The Atomics object provides atomic operations such as add, sub, and, or, xor, load, store etc. as static methods. They are used with SharedArrayBuffer objects.

For more information and a good example: Source:https://www.slideshare.net/barakdrechsler/atomic-javascript

https://jirak.net/wp/introducing-new-javascript-optimizations-webassembly-sharedarraybuffer-and-atomics-in-edgehtml-16/

https://www.geeksforgeeks.org/atomics-or-in-javascript/

For good resource chapter 12 SharedArrayBuffer in:

Phang, C. L. (2017). An Effective Guide to Modern JavaScript:(ECMAScript 2017/ES 8). Chong Lip Phang.

<iframe frameborder="0" scrolling="no" style="border:0px" src="https://books.google.com.tw/books?id=--gvDwAAQBAJ&lpg=PP3&ots=zqgshUnSQa&dq=atomic%20%20SharedArrayBuffer%20%20for%20javascript&lr&pg=PA80&output=embed" width=500 height=500></iframe>
like image 22
I_Al-thamary Avatar answered Oct 14 '22 03:10

I_Al-thamary