Node.js · Senior · Question 15 of 75
What is a good way to share large binary data between worker threads without copying?
- 1Write it to a temporary file and read it in each worker
- 2Convert it to a JSON string and send it with
postMessage - ✓Correct answer: Transfer an
ArrayBufferin thetransferListor use aSharedArrayBufferwithAtomicsfor synchronization
Explanation
Transferring moves ownership: the sender can no longer use the buffer. SharedArrayBuffer lets both sides read and write the same memory, so use Atomics to avoid races.