Transferable and shared buffers
Prerequisites: previous curriculum lessons on Node streams, HTTP, buffers and asynchronous errors. Local lab baseline: Node.js 22.16.0, Linux, ESM. Use a disposable directory. Static examples are not ByteMind interactive execution. Each example is independent; save the named files together and run node main.mjs. The author harness imposes a five-second process deadline.
How it works
A transfer list can move an ArrayBuffer to another thread, detaching the sender’s buffer and its views. Use explicitly allocated ArrayBuffers for predictable ownership; pooled Node Buffers may share underlying allocations and have different transfer behavior. SharedArrayBuffer instead permits both workers to access the same memory, so coordination matters. Atomics provides atomic operations on suitable integer typed arrays; a sequence of separate atomic operations does not automatically make a compound invariant atomic. Shared memory does not imply shared JavaScript objects or a sandbox.
Transfer a dedicated ArrayBuffer to a worker
import {Worker} from 'node:worker_threads';import {once} from 'node:events';const buffer=new ArrayBuffer(4);new Uint8Array(buffer).set([1,2,3,4]);const worker=new Worker(new URL('./worker.mjs',import.meta.url));try {const reply=once(worker,'message');worker.postMessage(buffer,[buffer]);console.log('sender bytes',buffer.byteLength);console.log('receiver sum',(await reply)[0]);}finally{await worker.terminate();}import {parentPort} from 'node:worker_threads';parentPort.once('message',buffer=>parentPort.postMessage(new Uint8Array(buffer).reduce((a,b)=>a+b,0)));Expected output:
sender bytes 0receiver sum 10The sender is detached immediately; the worker reads the transferred bytes.
Update one shared counter with atomic operations
import {Worker} from 'node:worker_threads';import {once} from 'node:events';const memory=new SharedArrayBuffer(4);const counter=new Int32Array(memory);const workers=[1,2].map(()=>new Worker(new URL('./worker.mjs',import.meta.url),{workerData:memory}));try {await Promise.all(workers.map(w=>once(w,'message')));console.log(Atomics.load(counter,0));}finally{await Promise.all(workers.map(w=>w.terminate()));}import {parentPort,workerData} from 'node:worker_threads';const counter=new Int32Array(workerData);for(let i=0;i<100;i++)Atomics.add(counter,0,1);parentPort.postMessage('done');Expected output:
200Each worker performs 100 atomic increments; the final sum is invariant under their execution order.
Common mistakes
Do not read a transferred view as though it still owns bytes. Do not rely on unsynchronized increments to shared counters or assume Atomics makes arbitrary multi-step transactions safe.
Practice
Predict the sender byteLength after transferring an eight-byte buffer. Explain how the answer changes with a SharedArrayBuffer.
Further reading
Official documentation: worker_threads. Internal reference candidate: node-transferable-and-shared-buffers-lookup (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Transferable and shared buffers — knowledge check
Apply the lesson to a fresh scenario and explain the resource or protocol boundary.
What happens to an eligible ArrayBuffer after transfer?
What does SharedArrayBuffer allow?
Why prefer Atomics.add for the demonstrated counter?