JavaScript · Senior · Question 64 of 75
Why do schedulers (e.g. React's) use MessageChannel instead of setTimeout(fn, 0)?
- ✓Correct answer: Posting a message schedules a macrotask without the ~4ms clamping of nested timers
- 2
MessageChannelruns code in a worker - 3
setTimeoutdoesn't run in background tabs
Explanation
Nested setTimeout calls are clamped to at least 4ms by the HTML spec. A message event yields to the browser for rendering and input, then continues quickly.