JavaScript · Senior · Question 13 of 75
Why pass an ArrayBuffer to a worker as transferable: postMessage(buf, [buf])?
- ✓Correct answer: Ownership is transferred without copying; the buffer becomes empty in the source thread
- 2The buffer becomes shared, and both threads see each other's changes
- 3The buffer is compressed before sending, which reduces memory use
Explanation
Copying large data is expensive. A transfer hands over the memory instantly, and the sender's byteLength becomes 0. True shared memory is SharedArrayBuffer with Atomics, but it requires cross-origin isolation (COOP/COEP headers).