So far, we've see how to send data between the main thread and workers. The downside of how we've done this so far is that the data is copied. This means that as the amount of data and number of workers increase, so does the amount of copying that has to be done. Luckily, there is a way to share data between threads with less overhead.
SharedArrayBuffer
can be shared between arrays without copying data. In this recipe, we'll see how to create, as well as read and write, data from a SharedArrayBuffer
.
This recipe assumes you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
- Open your command-line application, and navigate to your workspace.
- Create a new folder named
05-05-creating-shared-array-buffer
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
.
- Create a
main.js
file with a main method that defines constants forNUM_COUNT
,BYTES_FOR_32_BIT...