Course contentsShow
← JavaScript
Lesson 2180 of 2,72345. Workers and ConcurrencyFree lesson

What is SharedArrayBuffer?

Understand SharedArrayBuffer as a way to share raw binary memory between multiple workers or threads.

What is SharedArrayBuffer?

You'll learn: What SharedArrayBuffer is and why it enables true shared memory between workers or threads.

The Core Idea

SharedArrayBuffer is a special type of ArrayBuffer that allows multiple workers or threads to access the exact same block of raw binary memory simultaneously. Unlike regular data sent via postMessage (which creates copies), SharedArrayBuffer lets workers read and write to a single shared memory location—enabling true concurrent access.

Think of it like a shared whiteboard in a conference room. Instead of passing notes back and forth (copying data with postMessage), everyone in the room can write on and read from the same whiteboard at the same time. This eliminates the copying overhead but requires coordination to avoid chaos.

Why It Matters

When you send data between workers using postMessage, JavaScript creates a copy of that data (via structured clone or transfer). For large datasets or high-frequency updates, this copying becomes a performance bottleneck.

With SharedArrayBuffer, workers share direct access to the same memory. Changes made by one worker are immediately visible to all others—no copying, no delays. This is essential for performance-critical scenarios like parallel processing of large datasets, real-time simulations, or coordinating state between multiple workers.

The Trade-off

Shared memory is powerful but dangerous. Multiple workers accessing the same memory simultaneously can cause race conditions if not carefully synchronized. You'll need techniques like Atomics (covered later) to coordinate safe access.

Key Takeaway: SharedArrayBuffer provides zero-copy shared memory access between workers, enabling true parallelism but requiring careful synchronization to prevent data races.