How it works...

It feels a little unnatural to be thinking about byte size when working in JavaScript, but it's necessary for working with SharedArrayBuffer. The preceding example creates an array of 2,048 32-bit integers. So in order to create a SharedArray buffer we have to calculate how much memory 2,048 32-bit integers will take up. This is done with simple multiplication. Once we have ARRAY_SIZE we can allocate the memory.

Now that we have the array, we need to cast it as some type in order to read and write from it. We're using Int32Array, so when we perform array access operations the values will be cast as 32-bit integers.

When filling the array, we simply loop over each number and multiply a random number by the MAX_NUMBER; the resulting ...

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.