Using the Atomics.sub(typedArray, index, value) method

Atomics.sub is a way to subtract a particular value from a particular index in a typed array. It is also fairly simple to use:

const sab = new SharedArrayBuffer(1);const arr = new Uint8Array(sab);arr[0] = 5;console.log(Atomics.sub(arr, 0, 2));console.log(Atomics.load(arr, 0));

Atomics.sub is, again, a thread-safe way of doing arr[0] -= 2.

This outputs:

53
 Atomics.sub returns the old value at that index. The value is updated at that index after the command is run.

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.