SharedArrayBuffer and shared views
Quick answer
SharedArrayBuffer allows multiple agents to access the same underlying bytes. It is not an ownership-transfer mechanism.
How it works
Create new SharedArrayBuffer(byteLength) and interpret it with typed arrays. Structured cloning of a shared buffer can create a distinct wrapper referring to the same data. Do not put a shared buffer in a transfer list. Browser exposure generally requires a secure, cross-origin isolated environment with appropriate response headers and policy. Check crossOriginIsolated and actual API availability. The following examples use Node.js; they show shared storage, not browser header configuration or a concurrent race test.
Local examples
Run with Node.js 22.16.0. Browser shared-memory exposure is not tested here.
(async () => {const b=new SharedArrayBuffer(4);const a=new Int32Array(b),c=new Int32Array(b);a[0]=6;console.log(c[0],b.byteLength);})()Expected console output:
6 4Both views observe the same bytes in this sequential Node example.
Run with Node.js 22.16.0. Browser shared-memory exposure is not tested here.
(async () => {const a=new SharedArrayBuffer(4),b=structuredClone(a);new Int32Array(b)[0]=11;console.log(a===b,new Int32Array(a)[0]);})()Expected console output:
false 11The wrappers differ but access the same memory.
Common mistakes
Do not treat concurrent ordinary writes as a synchronization protocol. Use Atomics and design clear ownership of shared state.
Practice
Compare two views of a shared buffer and a copied ordinary buffer. Explain why single-agent tests do not prove concurrent correctness.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
SharedArrayBuffer and shared views — Knowledge Check
Apply the concept and reason about local example output.
What distinguishes a shared buffer from transferred ordinary ArrayBuffer ownership?
Which browser condition should be verified before relying on SharedArrayBuffer?
Does sequential shared-view output prove concurrent code is race-free?
What is the expected console output of SharedArrayBuffer and shared views example 1?
(async () => {const b=new SharedArrayBuffer(4);const a=new Int32Array(b),c=new Int32Array(b);a[0]=6;console.log(c[0],b.byteLength);})()What is the expected console output of SharedArrayBuffer and shared views example 2?
(async () => {const a=new SharedArrayBuffer(4),b=structuredClone(a);new Int32Array(b)[0]=11;console.log(a===b,new Int32Array(a)[0]);})()