Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

SharedArrayBuffer — shared binary memory

SharedArrayBuffer holds bytes that different JavaScript agents can access. Views interpret those bytes, while Atomics helps coordinate concurrent operations.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

new SharedArrayBuffer(byteLength)
new SharedArrayBuffer(byteLength, { maxByteLength })

SharedArrayBuffer represents a block of shared binary memory, not an array of numbers. Its constructor requires new; the length is in bytes, initially zero-filled. Read and write through views such as Int32Array or DataView. Unlike ArrayBuffer, this buffer cannot become detached.

Sharing alone does not provide synchronization. Operations such as view[0]++ are not atomic increments: concurrent updates can be lost. Use Atomics on supported integer views and a deliberate communication protocol; several atomic calls do not automatically form a single transaction.

Without options, the buffer has a fixed length. The { maxByteLength } option creates a buffer that can grow through grow(newByteLength), but cannot shrink. byteLength is the current size; maxByteLength is the limit.

Availability depends on the host: the specification permits omitting the constructor. ECMAScript alone does not establish support in a particular browser or its security requirements; execution in Node does not prove those either. The example checks availability and uses two views of the same buffer without launching concurrent agents.

Examples

if (typeof SharedArrayBuffer === "undefined") {
  throw new Error("SharedArrayBuffer unavailable");
}

const buffer = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT);
const first = new Int32Array(buffer);
const second = new Int32Array(buffer);

Atomics.store(first, 0, 10);
const previous = Atomics.add(second, 0, 5);
console.log(`${buffer.byteLength} ${previous} ${Atomics.load(first, 0)}`);

Result

4 10 15

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources