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.
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.