Performance: SharedArrayBuffer for shared memory
MEDIUM IMPACT
This affects how efficiently multiple threads or workers share and access memory without copying data, improving concurrency and responsiveness.
const { Worker, isMainThread, workerData } = require('worker_threads');
const sharedBuffer = new SharedArrayBuffer(1024);
if (isMainThread) {
const worker = new Worker('./worker.js', { workerData: sharedBuffer });
} else {
const shared = new Uint8Array(workerData);
// Access shared memory directly without copying
}const { Worker } = require('worker_threads');
const buffer = new ArrayBuffer(1024);
const worker = new Worker('./worker.js', { workerData: buffer });
// Worker copies the buffer, no real shared memory| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| ArrayBuffer passed to worker | N/A | N/A | N/A | [X] Bad |
| SharedArrayBuffer passed to worker | N/A | N/A | N/A | [OK] Good |