How it works...

As mentioned previously, SharedArrayBuffer is not copied between threads. It is shared. So, when we create the shared array buffer and pass that reference to a worker, the values inserted into the array by the main thread are available in the worker.

After the worker receives messages to perform calculations, the values can be accumulated as they were in any other array, and the value sent back is a simple message.

Get ECMAScript Cookbook 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.