How to Use Worker Threads in Node.js for CPU-Heavy Work
Worker threads let you run CPU-heavy code off the main thread. Here is how to use them.
How to Use Worker Threads in Node.js for CPU-Heavy Work
Worker threads let you run CPU-heavy code off the main thread, keeping the event loop free. Here is how to use them.
What Worker Threads Are
Node.js's worker_threads module lets you run JavaScript on separate threads. This is for CPU-heavy work that would block the main thread's event loop.
When to Use Worker Threads
For heavy computation: image processing, cryptographic computation, large data processing, machine learning inference. Anything that takes more than a few milliseconds of synchronous CPU time should be offloaded.
How to Create a Worker
Import { Worker } from 'worker_threads'. Create a new Worker with a file path. The worker runs the file on a separate thread. Communicate via messages using postMessage and on('message').
Passing Data
Workers can receive data via postMessage and send results back. Large data is copied by default, but you can use SharedArrayBuffer for zero-copy sharing between the main thread and workers.
Error Handling
Workers can throw errors. Listen for the 'error' event on the worker to catch them. An uncaught error in a worker crashes the worker but not the main thread.
When Not to Use Worker Threads
For I/O-bound work, async APIs are better. Workers are for CPU-heavy computation. Using workers for I/O adds overhead without benefit, since async I/O already keeps the event loop free.
The Takeaway
Use worker_threads for heavy CPU computation: create a Worker with a file, communicate via postMessage, handle errors with the 'error' event, and use SharedArrayBuffer for zero-copy large data. Do not use them for I/O-bound work.
Import { Worker } from 'worker_threads', create a new Worker with a file path, communicate via postMessage and on('message'), handle errors with the 'error' event, and use SharedArrayBuffer for zero-copy large data sharing.
For heavy CPU computation: image processing, cryptographic computation, large data processing, machine learning inference. Anything that takes more than a few milliseconds of synchronous CPU time should be offloaded to keep the event loop free.
For I/O-bound work. Async APIs (fs.readFile, fetch, database queries) already keep the event loop free. Using worker threads for I/O adds overhead without benefit, since the I/O is delegated to libuv anyway.
Use postMessage to send data, which copies it by default. For large data, use SharedArrayBuffer for zero-copy sharing between the main thread and workers, which avoids the overhead of copying.
No. An uncaught error in a worker crashes the worker thread but not the main thread. Listen for the 'error' event on the worker to catch and handle worker errors gracefully in the main thread.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

