Node.js FAQ: Top Questions
25. What are worker threads in Node.js?
Worker threads , introduced in Node.js 10 (stable in 12), allow running JavaScript in parallel threads, bypassing the single-threaded event loop for CPU-intensive tasks.
-
Module:
worker_threads
, providesWorker
,parentPort
. -
Communication:
Message passing via
postMessage
andon("message")
. - Limitations: Not for I/O-bound tasks; overhead for thread creation.
- Use Case: CPU-heavy computations (e.g., crypto, image processing).