Pass data to a Web Worker and transfer an ArrayBuffer efficiently

Tests postMessage clone versus transferable objects for zero-copy transfer. Good answers note postMessage copies by default, but ArrayBuffer can move via transfer list, neutering the original.
Tests whether you understand Web Worker data sharing and the cost of cloning versus zero-copy transfer. A strong answer covers that postMessage uses structured clone by default, duplicating data. For large ArrayBuffers, pass the buffer in the transfer list as the second argument; this moves the underlying memory to the worker, detaching it from the original so its byteLength becomes zero and access throws. Red flag: omitting the transfer list, confusing TypedArray serializability with transferability, or missing that the original loses access.
Read the original → developer.mozilla.org
- #web workers
- #arraybuffer
- #transferable objects
- #postmessage
- #structured clone
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.