SharedArrayBuffer

SharedArrayBuffer is similar to ArrayBuffer, in a way that it can be used to create views on shared memory, so that multiple threads can read and write the same data in memory. Unlike an ArrayBuffer, a SharedArrayBuffer cannot become detached.

APIs accepting SharedArrayBuffer objects:

-WebGLRenderingContext.bufferData()

-WebGLRenderingContext.bufferSubData()

-WebGL2RenderingContext.getBufferSubData()

(for more info, read the example at 8.13.4)

Like ArrayBuffer (13.1), a SharedArrayBuffer can be made growable since ECMAScript 2024, by passing a maxByteLength option when constructing it. Its .grow(newByteLength) method can then only ever increase .byteLength (up to that cap) — never shrink it — which matters because, unlike a resizable ArrayBuffer, a SharedArrayBuffer may simultaneously be visible to other agents (eg. Worker threads) that could be reading it at any moment.


var sab = new SharedArrayBuffer(8, {maxByteLength: 1024});
console.log(sab.growable);
console.log(sab.maxByteLength);

sab.grow(64);
console.log(sab.byteLength);

true 1024 64