ESC

Type to search the knowledge base.

Idle Work Scheduling

requestIdleCallback, idle deadlines, and what work is safe to defer without breaking UX or analytics.

advanced3 min read
  • performance
  • requestidlecallback
  • scheduling
  • inp
  • main-thread

Not all main-thread work must run now. Idle scheduling runs deferrable tasks when the browser has spare time, protecting input and animations. The historic API is requestIdleCallback; newer scheduling primitives complement it.

Docs: MDN requestIdleCallback, web.dev idle-until-urgent, scheduler.yield.

requestIdleCallback

function enrichAnalytics(deadline) {
  while (deadline.timeRemaining() > 0 && queue.length) {
    process(queue.shift());
  }
  if (queue.length) {
    requestIdleCallback(enrichAnalytics, { timeout: 2000 });
  }
}

requestIdleCallback(enrichAnalytics, { timeout: 2000 });
Parameter Role
timeRemaining() Estimated idle ms left in the period
didTimeout Fired because timeout expired
timeout option Force run even under load

Good idle candidates

  • Non-critical analytics enrichment
  • Prefetching next-route data
  • Warming non-critical components
  • Indexing client-side search
  • Cleanup / telemetry compression

Bad idle candidates

  • Paint-critical UI updates after click
  • Anything user is waiting on
  • Work that must complete before unload without a timeout
  • Continuous loops that never yield (still need chunking)

Safari / support

requestIdleCallback support has been uneven historically. Polyfill pattern:

const ric =
  window.requestIdleCallback ||
  function (cb) {
    const start = Date.now();
    return setTimeout(() => {
      cb({
        didTimeout: false,
        timeRemaining: () => Math.max(0, 50 - (Date.now() - start)),
      });
    }, 1);
  };

Polyfills approximate; don’t require perfect idle for correctness — use timeout.

Idle vs yield during urgent work

When handling a click, you need yield so paint can happen (scheduler.yield, setTimeout(0)), not idle — idle may not run while the user is active. See Long tasks and yielding.

Interaction with INP

Deferring third-party boot to idle after first paint often improves load and early interactions. Don’t schedule huge idle tasks that become long tasks — still chunk.

Interview out-loud

“requestIdleCallback runs deferrable work with timeRemaining and an optional timeout. I use it for analytics and prefetch, never for the next paint after input. For mid-handler breaks I yield, not idle.”

How this shows up in interviews

Be ready to define the metric or technique in one sentence, name one measurement approach (DevTools panel, web-vitals, or headers), and cite a concrete fix you would try first. Walk through a before/after: what the waterfall or flame chart showed, what you changed, and which percentile moved. Mention a tradeoff (complexity, caching correctness, or third-party business constraints) so the answer doesn’t sound like a blog checklist.

Production guardrails

Ship behind a flag when the change is risky, watch field p75 for the affected template for at least a few days, and keep a rollback path. Pair lab verification (throttled Performance/Network) with RUM so you don’t celebrate a Lighthouse-only win. Document the owner of any ongoing budget or third-party exception.

Further depth

Teams often under-invest in this topic until an incident or CWV regression. Schedule a one-hour drill: reproduce the failure mode in DevTools, list the top three mitigations for your stack, and file tickets with owners. Revisit after the next major feature that touches networking, rendering, auth, or third parties — those are the moments regressions land. Keep primary documentation links in the runbook so on-call is not searching chat history at 2am.

Concrete artifacts to leave behind: a short architecture note, a CI assertion or header snapshot, and a dashboard panel (lab or field) that would have caught the last bug. Teaching the rest of the team the mental model matters as much as the one-line fix.

Further reading

Related guides