Posting an ArrayBuffer to a worker copies or transfers it; posting a SharedArrayBuffer shares the same memory, so threads read and write one block without messages. It is never detached. Since ES2024 { maxByteLength } makes it growable, but grow() cannot shrink, because another thread may be reading the end at that moment.
import { Worker } from 'node:worker_threads';
const sab = new SharedArrayBuffer(4, { maxByteLength: 64 });
const ints = new Int32Array(sab); // length-tracking view
const code = `const { workerData } = require('node:worker_threads');
new Int32Array(workerData)[0] = 42;`;
new Worker(code, { eval: true, workerData: sab }).on('exit', () => {
sab.grow(12);
console.log(ints[0], ints.length, sab.byteLength, sab.growable);
});Output
42 3 12 true
Browsers disabled it in January 2018, since shared memory makes a precise timer for Spectre attacks, and now expose it only when crossOriginIsolated is true: serve over HTTPS with Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp, then check that embeds and CDN assets still load (Dedicated Web Workers).