Worker threads — what do you do with an expensive computation?
Answer out loud or in your head first, then compare.
Answer
Worker threads run JavaScript on extra threads and are useful for expensive CPU work. The main event loop can then keep handling I/O.
Example: I move a large report calculation to a pool of workers and pass the job through messages. I don't create a new worker for every small operation.
To be precise: a child process has its own process and memory. Workers can share memory through SharedArrayBuffer, but the simplest model is messages with copied or transferred data.
Pitfall: plain async doesn't move CPU work to a worker. For typical I/O an extra worker often doesn't help, because Node already has asynchronous mechanisms.
How did it go? Be honest — “Not yet” puts the question on your review list.
Related questions
- B01 · What blocks the event loop, and why doesn't async move computation to another thread? — JavaScript, TypeScript and Node.js