Skip to content

QuizNot attempted

Microtask starvation

Why can an endless chain of microtasks freeze the page while an endless chain of setTimeout(fn, 0) does not?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • The browser drains the whole microtask queue before it renders or takes the next task, so new microtasks keep it from ever reaching rendering
  • After every task the event loop drains the microtask queue until it is EMPTY, including microtasks queued while draining, so an endless chain never lets the loop reach rendering or input
  • each setTimeout callback is a separate task, and the browser can render between tasks.
  • Not 'timers run on another thread': timer callbacks run on the main thread too, only the countdown is elsewhere.
  • Not '1 call per second': nested timers are clamped to about 4ms, not 1s.
  • Not OS CPU priority: it is event-loop ordering, the OS scheduler isn't involved.