Skip to main content
InterviewHubPRO
Section Overview
Q16

Question 16 of 50

Part 2: Core Architecture & Implementation (Mid-Level)

32%
Web Vitals & Performance (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)

How does `scheduler.yield()` break up Long Tasks to improve INP?

senior LevelCode ExampleSystem Design

In Simple Words: A JavaScript loop running for 300ms locks the main thread, delaying all user clicks. Historically developers used setTimeout(..., 0) which adds unpredictable 4ms timer clamp delays. scheduler.yield() (Web Standard): Yields control back to the browser's event loop to paint frames and process pending user clicks, then immediately resumes execution:

javascript
async function processMassiveList(items) {
  for (let i = 0; i < items.length; i++) {
    computeItem(items[i]);

    // Check if main thread has been running for > 50ms
    if (navigator.scheduling?.isInputPending?.() || i % 100 === 0) {
      await scheduler.yield(); // Pauses, lets user click process, then resumes!
    }
  }
}