Careeroza
Event Loop in Depth
The Full Picture
| Part | Role |
|---|---|
| Call stack | runs JavaScript, one frame at a time |
| Web APIs / libuv | timers, network and I/O done outside the JS thread |
| Microtask queue | promise reactions; drained completely after each task |
| Task queue | timers, events, I/O callbacks; one per loop turn |
| Rendering | in browsers: style, layout and paint between tasks (~every 16 ms) |
Blocking the Main Thread
Example
Long synchronous work delays everything else, including timers that are already due:
const start = Date.now();
setTimeout(() => console.log("timer ran after ~" + Math.round((Date.now() - start) / 100) * 100 + " ms (asked for 0)"), 0);
while (Date.now() - start < 300) {} // busy loop blocks the thread
console.log("busy work done");Output
busy work done timer ran after ~300 ms (asked for 0)
Tip: Split heavy work into chunks, or move it to a Web Worker (browser) / worker thread (Node) so the UI stays responsive.