Need Assistance?

support@tutorialshub.in

Advanced Node.js

Worker threads

Node.js Free Lesson
5 min read
ADVERTISEMENT

CPU-heavy JavaScript can block the event loop. Node provides worker_threads so expensive JavaScript calculations can run in separate threads.

Use workers for suitable CPU-bound tasks. Normal database and network I/O generally does not require worker threads.

Deep dive

When not to use workers

Do not add workers simply because an application uses asynchronous code. Worker threads are primarily useful when JavaScript itself performs significant CPU-bound work.

Communication

Workers can exchange messages with the main thread. Design the messages clearly and avoid sending unnecessarily large data structures.

ADVERTISEMENT