Event Loop and Concurrency
How JavaScript stays responsive on a single thread: one call stack, separate queues of pending jobs, and a loop that decides what runs next. Interviewers lean on this pillar because 'what does this log, and in what order?' cleanly separates people who memorized async syntax from people who understand the scheduler underneath it.
part ofJavaScriptoverview, primer and where to startread it →on this pageshowhide
explore
- Call Stack and Run-to-Completion4 questions
- Macrotask Queue and Timers13 questions
- setTimeout Delays, Clamping, and Throttling5 questions
- setInterval Drift and Self-Scheduling Timers4 questions
- setTimeout(0) and Yielding to the Loop4 questions
- Microtask Queue9 questions
- Promise Jobs and the Microtask Checkpoint5 questions
- Microtask Starvation4 questions
- Output Ordering Puzzles5 questions
questions
page 2 of 2A colleague made a long job responsive by calling setTimeout(next, 0) after every single item. Each item takes microseconds, yet the job now processes only a few hundred items per second. What is going on, and how would you fix it?
level: seniorimportance: nice to knowfreq 33%
basics
~20 sA zero delay is not zero. Browsers clamp deeply nested zero-delay timers to about 4 ms and Node raises a delay below 1 ms to 1 ms, so a per-item timer chain is capped at a few hundred to a thousand hops per second. Batch by a time budget and yield once per batch instead.
showing 31–31 of 31