NodeJS: from single thread to worker threads
summary
Covers why CPU work blocks Node’s single-threaded event loop and compares hacks: breaking work with setImmediate, spawning processes with child_process/cluster, and true threads via worker_threads. Shows API details: worker_threads stable in Node 12, options like workerData/env/eval/resourceLimits, message ports vs cloning (cloning 10MB of nested objects can cost ~47ms in one benchmark), transferable ArrayBuffer and sharedArrayBuffer + Atomics for synchronized shared memory, and a concrete four-worker pool Sudoku solver that reuses threads and returns results to the main server.
AI-written summary · about
chapters (7)
- 0:00Start
- 3:55Golden Rule Don't block the event loop
- 4:35Running CPU Intensive tasks
- 5:52Offloading Child Process Cluster
- 11:57What makes Worker Threads special
- 16:12MessageChannel async two-way communication channel between threads
- 31:13Recommendation • Use worker thread pools • Do not use them for parallelizing I/O
related talks
33mNode.js: The Road to Workers
26mA Crash Course on Worker Threads
13mConcurrency vs Parallelism in Node.js: Understanding the Event Loop & Worker Threads
27mA Crash Course On Worker Threads
34mNode.js: The Road to Workers
22mScaling Node.js and unblocking the Event Loop
28mNode.js: The Road to Workers
32mNODE.JS WORLD
