CodePark/ questions / Workers, cluster & child processes
START PRACTICINGPRACTICE
Backend question · Node.js › Workers, cluster & child processes

How is data passed between the main thread and a worker thread, and what are the performance implications?

By default, postMessage sends a copy of the data. It makes the copy with the structured clone algorithm. This supports objects, arrays, Map, Set, dates, typed arrays and more. It does not support functions or class prototypes. Copying a large object costs CPU on both sides. For small tasks, this cost can cancel out the benefit of moving work to a worker.

You can avoid the copy in two ways:

  • Transfer: put an ArrayBuffer or MessagePort in the transfer list. Ownership moves to the receiver, and the sender can no longer use that buffer.
  • Share: use a SharedArrayBuffer when both sides need to read and write the same memory. Coordinate access with Atomics.

Starting a worker has a real cost. So create a pool of workers once and reuse it for many tasks. Use a library such as Piscina or your own queue. Do not start a new worker for each request.

  • postMessage copies data with the structured clone algorithm.
  • Structured clone cannot copy functions or prototypes, and large copies cost CPU.
  • Transferring an ArrayBuffer or MessagePort moves it without copying.
  • A SharedArrayBuffer with Atomics gives both sides shared memory.
  • Reuse a pool of workers instead of starting one worker per task.