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.