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
ArrayBufferorMessagePortin the transfer list. Ownership moves to the receiver, and the sender can no longer use that buffer. - Share: use a
SharedArrayBufferwhen both sides need to read and write the same memory. Coordinate access withAtomics.
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.
postMessagecopies data with the structured clone algorithm.- Structured clone cannot copy functions or prototypes, and large copies cost CPU.
- Transferring an
ArrayBufferorMessagePortmoves it without copying. - A
SharedArrayBufferwithAtomicsgives both sides shared memory. - Reuse a pool of workers instead of starting one worker per task.
Read more