Sending SharedArrayBuffer to a Web Worker
Now that we know how to create and use a SharedArrayBuffer
, we can use one to share data between the main thread and workers. As mentioned previously, this has an advantage over posting JavaScript objects, in that the data doesn't need to be copied; it is shared.
In this recipe, we'll see how to share a SharedArrayBuffer
with a worker, and post the result back to the main thread.
Getting ready
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. It also assumes that you have enabled shared memory in your browser. If you haven't please see the recipes at the beginning of this chapter.
How to do it...
- Open your command-line application, and navigate to your workspace.
- Create a new folder named
05-06-sending-shared-array-to-worker
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - Create a
main.js
file with anonMessage
function...