Sitemap

SharedArrayBuffer | Node.js

3 min readSep 30, 2024

--

Press enter or click to view image in full size

SharedArrayBuffer is a powerful feature in JavaScript that enables the sharing of binary data between different execution contexts, such as web workers or Node.js worker threads. This allows multiple threads to read and write to the same memory buffer concurrently, facilitating high-performance parallel processing.

SharedArrayBuffer is a special type of binary data buffer in JavaScript that allows multiple threads (such as web workers or Node.js worker threads) to access and modify the same memory space concurrently. Unlike ArrayBuffer, which creates a separate copy of the data for each thread, SharedArrayBuffer enables true shared memory, making it possible to implement efficient parallel algorithms and high-performance applications.

Press enter or click to view image in full size

Use Cases for SharedArrayBuffer

SharedArrayBuffer is particularly useful in scenarios that require high-performance concurrent processing. Some common use cases include:

  1. Parallel Computing: Distributing computational tasks across multiple threads to leverage multi-core processors.
  2. Real-Time Data Processing: Handling real-time data streams, such as audio or video processing, where low latency is crucial.
  3. Game Development: Managing game state and physics calculations across multiple workers for smoother performance.

How to Use SharedArrayBuffer

Using SharedArrayBuffer involves creating the buffer, sharing it between workers, and synchronizing access to prevent data races and ensure consistency.

Creating a SharedArrayBuffer

You can create a SharedArrayBuffer by specifying its size in bytes:

// Create a SharedArrayBuffer of 1024 bytes
const sharedBuffer = new SharedArrayBuffer(1024);

// Create a view for easier data manipulation, e.g., a 32-bit integer array
const int32View = new Int32Array(sharedBuffer);

Sharing the Buffer Between Workers

To share the SharedArrayBuffer between workers, you can pass it when creating a new worker. In Node.js, you can use the worker_threads module, and in browsers, you can use the postMessage API.

Node.js Example:

// main.js

const { Worker } = require('worker_threads');

// Create a SharedArrayBuffer
const sharedBuffer = new SharedArrayBuffer(1024);
const int32View = new Int32Array(sharedBuffer);

// Initialize shared data
int32View[0] = 0;

// Create a worker and pass the SharedArrayBuffer
const worker = new Worker('./worker.js', { workerData: sharedBuffer });

worker.on('message', (msg) => {
console.log(`Main thread received: ${msg}`);
});

worker.postMessage('Start processing');
//worker.js

const { parentPort, workerData } = require('worker_threads');

// Access the SharedArrayBuffer
const int32View = new Int32Array(workerData);

// Perform some operations
for (let i = 0; i < 1000; i++) {
Atomics.add(int32View, 0, 1);
}

// Notify the main thread
parentPort.postMessage(`Counter value: ${int32View[0]}`);

Synchronizing Access with Atomics

When multiple threads access the same SharedArrayBuffer, it's essential to synchronize their operations to prevent data races and ensure data integrity. JavaScript provides the Atomics object for this purpose, which offers atomic operations on shared memory.

Common Atomics Methods:

Press enter or click to view image in full size

Example: Implementing a Simple Mutex

const mutex = new Int32Array(sharedBuffer);
mutex[0] = 0; // 0: unlocked, 1: locked

function lock() {
while (Atomics.compareExchange(mutex, 0, 0, 1) !== 0) {
Atomics.wait(mutex, 0, 1);
}
}

function unlock() {
Atomics.store(mutex, 0, 0);
Atomics.notify(mutex, 0, 1);
}

// Usage in a worker or main thread
lock();
// Critical section: perform operations on sharedBuffer
unlock();

Conclusion

SharedArrayBuffer is a powerful tool in JavaScript that enables efficient, high-performance concurrent processing by allowing multiple threads to share and manipulate the same binary data buffer. When used correctly, it can significantly enhance the performance of applications that require parallelism, such as real-time data processing, game development, and large-scale data manipulation.

--

--

Aditya Yadav
Aditya Yadav

Written by Aditya Yadav

Software Engineer who talks about tech concepts in web development https://www.linkedin.com/in/aditya-yadav-01/