skip to content

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 pageshow

explore

questions

page 2 of 2

A 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 s

A 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.

open as a page

showing 31–31 of 31