Turning Learners Into Developers
Codekilla
CODEKILLA
back to course
Lesson 04 / 479%· free preview
Introduction to Node.js4/6

Single Thread vs Multi Thread

Single Threaded

Node runs your JavaScript on one main thread (the event loop). All synchronous code, all callbacks, all event handlers — they all share that one thread.

Why one thread isn't slow

While JS runs on one thread, I/O happens in C++ on libuv's thread pool or via the OS kernel asynchronously. So while JS is "idle" waiting for the disk or network, Node can accept thousands of other requests.

When the single thread bites you
js
// BAD — blocks the entire process for ~1 second
function fib(n) { return n < 2 ? n : fib(n-1) + fib(n-2); }
console.log(fib(42));   // every other request waits!
Multi-threading options
  • Worker Threads (built-in, since Node 10) — run JS in parallel threads, message-passing via postMessage.
  • Cluster module — spawn N child processes, share a port via the master.
  • child_process.spawn — run external programs (FFmpeg, Python, shell).
js
// worker.js — heavy fib in a thread
const { parentPort } = require('worker_threads');
parentPort.on('message', n => parentPort.postMessage(fib(n)));
Interview Questions

AI-powered recap

Quick recap quiz?

We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.

Ready to move on?
// feedback.matters()
Did this lesson help you?