The event loop · senior
Splitting heavy work: setTimeout, scheduler.yield and Web Workers
Any task over 50 ms is a long task that blocks clicks and paints, so heavy JavaScript must be split into chunks that yield back to the event loop (setTimeout, scheduler.yield() where supported, requestIdleCallback for low-priority work) or moved to a Web Worker.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- A long task is longer than 50 ms. While it runs, input waits, which hurts INP (INP replaced FID as a Core Web Vital in March 2024; good is 200 ms or less at the 75th percentile).
- async/await doesn't move work off the main thread. CPU work inside an async function still blocks.
- Yielding: await new Promise(r => setTimeout(r, 0)) lets input and rendering happen between chunks. scheduler.yield() resumes your work ahead of other tasks of the same priority, but MDN lists it as limited availability, so feature-detect it.
- Yielding with a microtask (await null, Promise.resolve()) does NOT let the browser render or handle input.
- requestAnimationFrame runs right before the next paint: use it for visual updates, not data crunching.
- requestIdleCallback runs in idle time; pass a timeout for work that must happen. MDN lists it as limited availability (Safari lacks it), so have a fallback.
- Move CPU-heavy work (parsing, sorting 50,000 rows, building a search index) to a Web Worker; data is copied with structured clone unless you transfer it.
- Measure with the DevTools Performance panel (long tasks, interactions track), the Long Animation Frames API and web-vitals attribution.
The red pen
Common traps: mistake → fix
Mistake: Wrapping heavy code in a Promise makes it non-blocking
Fix: it still runs on the main thread.
Mistake: await null yields to the browser
Fix: it is a microtask; no input or rendering happens in between.
Mistake: requestIdleCallback works everywhere
Fix: not in Safari, and without a timeout it may never fire under load.
Easy to hard
Questions interviewers ask, easy to hard
- You must process 50,000 rows on the client without freezing the UI. What are your options?
- What is a long task, and how does it relate to INP?
- requestAnimationFrame vs setTimeout for animation?
- Chunking with setTimeout vs scheduler.yield vs a Web Worker: trade-offs?
- How do you find which script causes a slow interaction in production?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.