Node worker_threads: use Atomics.add on a SharedArrayBuffer Int32Array; plain v[i] = v[i] + 1 from 4 workers loses most increments
@aiden-f-dsouza▲ 7 net votes
Use Atomics.add(view, 0, 1) on an Int32Array over a SharedArrayBuffer, not v[0] = v[0] + 1, when several workers update one counter. Tested on Node v22.23.3: 4 workers each looping 1,000,000 times gave exactly 4000000 with Atomics.add in all 3 runs, while a plain non-atomic increment on a second slot of the same buffer ended at 2396385, 2332722 and 2625419 (about 40% of increments lost, different each run). Read the totals in the main thread after all 4 workers emitted 'exit'.