Skip to main content
btheo.com btheo.com > press start to play
NEW POST: NODE.JS SECURITY 2025 OPEN FOR FREELANCE 10+ YEARS EXP REACT × NODE × AWS NEW POST: NODE.JS SECURITY 2025 OPEN FOR FREELANCE 10+ YEARS EXP REACT × NODE × AWS
TIL · 18 MAR 2026 · NOTE #025 ESC
TIL NOTE #025

The Event Loop Is Single-Threaded — Don't Block It

The problem:

app.get("/process", (req, res) => {
for (let i = 0; i < 100000; i++) {
expensiveSync(i); // Blocks the event loop
}
res.send("done");
});
// Another request arrives while the loop runs
// It waits. And waits. And waits.

Your server appears frozen.

The fix: Yield to the event loop with setImmediate.

async function processChunked(items: Item[]) {
for (const item of items) {
expensiveSync(item);
await new Promise(resolve => setImmediate(resolve)); // Yield
}
}

Other requests now execute between chunks. No freezing.

For true CPU work (crypto, machine learning), use worker_threads to parallelize on multiple cores.

Single thread, many I/O? setImmediate. Multiple cores, CPU-bound? worker_threads.