JavaScript SharedArrayBuffer

Beginner
8 min read
Updated: Nov 2026
3 examples
Shared memory

Definition and Usage

A SharedArrayBuffer is a block of raw memory that can be used by more than one thread at the same time. Normally, when you send data to a Web Worker, JavaScript copies it. With a SharedArrayBuffer, the main page and the workers all look at the same bytes, so nothing is copied and a change made by one thread is seen by the others.

Like an ArrayBuffer, it only stores bytes. To read and write them, you create a typed array such as Int32Array on top of it. To change shared numbers safely from several threads, use the Atomics methods.

This page explains the idea. The SharedArrayBuffer Methods index links the full guides for grow() and slice().

Browsers need cross-origin isolation.

For security, a browser only provides SharedArrayBuffer when the page sends the right headers. If typeof SharedArrayBuffer is "undefined" on your page, that is the reason. Node.js has no such rule.

JavaScript Syntax

Create a shared buffer, read and write it through a view, and try the two methods:

JavaScript
const buffer = new SharedArrayBuffer(8);   // 8 bytes of shared memory
const numbers = new Int32Array(buffer);    // a view: 2 numbers of 4 bytes

numbers[0] = 42;
console.log(Atomics.add(numbers, 1, 5));   // 0 (the old value)
console.log(buffer.byteLength);            // 8

const growable = new SharedArrayBuffer(8, { maxByteLength: 32 });
growable.grow(16);                         // make it bigger, in place
console.log(growable.byteLength);          // 16

const copy = growable.slice(0, 8);         // copy 8 bytes to a new buffer
console.log(copy.byteLength);              // 8

Core Ideas

Ideas every SharedArrayBuffer tutorial in this series builds on:

IdeaExampleRemember
SharedArrayBuffernew SharedArrayBuffer(8)A fixed amount of raw bytes that threads can share
byteLengthbuffer.byteLengthThe size of the buffer in bytes
Viewnew Int32Array(buffer)A typed array that reads and writes the shared bytes
AtomicsAtomics.add(view, 0, 1)Safe reads and writes when several threads work together
Growable buffer{ maxByteLength: 32 }A buffer that can grow later, up to a limit
grow()buffer.grow(16)Makes a growable buffer bigger in place; it never shrinks
slice()buffer.slice(0, 4)Copies a range of bytes into a new, separate buffer

SharedArrayBuffer vs ArrayBuffer

Both store raw bytes. The big difference is what happens when you send the buffer to another thread:

FeatureArrayBufferSharedArrayBuffer
Sent to a workerCopied, or moved with a transfer listShared: both threads see the same bytes
After sendingA moved buffer is detached (length 0)Still fully usable on both sides
Change the sizeresize() can grow and shrinkgrow() can only grow
Copy a rangeslice()slice() (the copy is not shared)
Safe updates from threadsNot neededUse Atomics
Available in browsersAlwaysOnly on cross-origin isolated pages

Use a normal ArrayBuffer when one thread owns the data. Use a SharedArrayBuffer when several threads must work on the same bytes. Example 3 shows the difference.

How SharedArrayBuffer Works

It is only memory

A SharedArrayBuffer has no items of its own, just a number of bytes. You create a typed array view to read and write it, and several views can look at the same buffer.

Sharing, not copying

When you send a SharedArrayBuffer to a worker with postMessage(), the worker receives a handle to the same memory. Nothing is duplicated, which makes it fast for large data.

Threads can collide

If two threads change the same number at the same time, one change can be lost. The Atomics methods make a read or a write happen in one safe step. Example 2 shows Atomics.add().

The size can grow, never shrink

A buffer created with maxByteLength is growable, and grow() adds zero bytes at the end. There is no way to make a shared buffer smaller.

Browsers ask for isolation

The page must send Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp. Then self.crossOriginIsolated is true and SharedArrayBuffer exists.

Real-Life Uses

You will rarely need shared memory in everyday code. It helps in a few special places:

UseHow shared memory helps
Big data in workersLet a worker process an image, audio or game world while the page keeps using the same bytes, without copying them.
Counters and flagsShare a progress counter or a "stop" flag between a worker and the page with Atomics.
Queues between threadsBuild a ring buffer in shared memory so one thread can hand work to another.
WebAssembly threadsThreaded WebAssembly programs keep their memory in a shared buffer.

JavaScript Examples

Three short programs. Each one prints plain text that is the same every time you run it, and they all run in Node.js.

Example 1 — Create a Shared Buffer and Look at It

Create a shared buffer, put two views on it, and see that a change through one view shows up in the other.

JavaScript
const buffer = new SharedArrayBuffer(8);
const first = new Int32Array(buffer);
const second = new Int32Array(buffer);

first[0] = 42;

console.log("typeof: " + typeof buffer);
console.log("byteLength: " + buffer.byteLength);
console.log("Int32 items: " + first.length);
console.log("Seen through second view: " + second[0]);
console.log("Instance of ArrayBuffer: " + (buffer instanceof ArrayBuffer));
grow() tutorial

How It Works

  1. Create the memory. new SharedArrayBuffer(8) gives 8 bytes, all zero. It is an object, but it is not an ArrayBuffer: it is its own type.
  2. Add views. Each Int32Array item uses 4 bytes, so 8 bytes make 2 items. Both views point at the same buffer.
  3. One memory, two views. Writing 42 through first is visible through second, because there is only one block of bytes.

Example 2 — Update a Shared Counter with Atomics

Use Atomics to add to a shared number. These calls are safe even when several threads use the same buffer.

JavaScript
const buffer = new SharedArrayBuffer(4);
const counter = new Int32Array(buffer);

for (let i = 0; i < 5; i++) {
  Atomics.add(counter, 0, 1);
}

console.log("Counter: " + Atomics.load(counter, 0));
console.log("Old value from add: " + Atomics.add(counter, 0, 10));
console.log("New value: " + Atomics.load(counter, 0));
console.log("Value from store: " + Atomics.store(counter, 0, 100));
Atomics tutorial

How It Works

  1. Add in one safe step. Atomics.add(counter, 0, 1) adds 1 to item 0. Unlike counter[0]++, no other thread can sneak in halfway through.
  2. Read safely. Atomics.load() reads the current value. Atomics.add() returns the old value, which is why the second line prints 5 and the next read shows 15.
  3. Store a value. Atomics.store() writes a number and returns that same number, so the last line prints 100.

Example 3 — See the Difference from ArrayBuffer

structuredClone() copies data the same way postMessage() does. Send an ArrayBuffer by moving it, and a SharedArrayBuffer by sharing it.

JavaScript
const normal = new ArrayBuffer(8);
const moved = structuredClone(normal, { transfer: [normal] });

console.log("ArrayBuffer after sending: " + normal.byteLength);
console.log("ArrayBuffer received: " + moved.byteLength);

const shared = new SharedArrayBuffer(8);
const received = structuredClone(shared);
new Int32Array(shared)[0] = 7;

console.log("SharedArrayBuffer after sending: " + shared.byteLength);
console.log("Received buffer sees the change: " + new Int32Array(received)[0]);
console.log("Same buffer object: " + (shared === received));
Methods index

How It Works

  1. A normal buffer is moved. With transfer, the bytes go to moved and the original is detached: its byteLength drops to 0.
  2. A shared buffer stays usable. After structuredClone(shared), the original still has 8 bytes. There is nothing to move, because both sides point at the same memory.
  3. The change is visible. The value 7 is written through the original and read through received. The two variables are different objects, but they are handles to the same bytes.

SharedArrayBuffer Methods

A SharedArrayBuffer has two methods of its own. Each one has its own tutorial:

MethodWhat it does
grow()Make a growable shared buffer bigger in place. It can never shrink.
slice()Copy a range of bytes into a new, separate SharedArrayBuffer.

Full list: SharedArrayBuffer Methods index.

Frequently Asked Questions

It is a block of raw memory that several threads, such as the main page and Web Workers, can use at the same time. You read and write it through typed arrays, and you use Atomics when several threads change the same values.
When you send an ArrayBuffer to a worker, it is copied or moved, and a moved buffer becomes unusable on the sending side. A SharedArrayBuffer is shared instead, so both sides keep using the same bytes. In browsers a SharedArrayBuffer also needs a cross-origin isolated page.
Browsers only provide it when the page is cross-origin isolated. The server has to send Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp. Then self.crossOriginIsolated is true. Node.js does not have this rule.
Only when several threads change the same values. Plain reads and writes can collide, and Atomics makes each step safe. If only one thread writes and the others read after a signal, simple code may be enough.
Yes, but only to grow. Create it with new SharedArrayBuffer(length, { maxByteLength }) and call grow(newLength) later. It can never get smaller. A normal ArrayBuffer can use resize() to grow and shrink.

Ready for the method guides?

Open the SharedArrayBuffer Methods index to learn grow() and slice() with syntax, runnable examples and FAQs.

SharedArrayBuffer Methods index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful