This post introduces the scheduler.yield
API and explains how you can use it to optimise the performance of your web applications. There are also interactive code demos you can follow along with.
Introduction
In the context of web performance, scheduling is the browser's way of deciding which tasks to run (JavaScript, page rendering, etc.) and when to run them. Scheduling is a key concept that can impact the user experience of your web applications.
While not every website needs to worry about scheduling, scheduling is an important feature to understand as it offers precise timing and control over how your JavaScript is scheduled.
What is scheduler.yield
?
The scheduler.yield
API is supported in Chromium-based browsers.
The scheduler.yield()
method is used to yield control back to the browser's scheduler, allowing other important tasks to run. It's the browser that decides what's considered "important".
scheduler.yield(); // Promise {<pending>}
This can be useful when you want to ensure that your JavaScript code doesn't block the main thread and negatively impact the user experience.
You might be wondering, what other work does the browser need to do? The browser scheduler is responsible for a lot of things, such as:
- Rendering the page
- Garbage collection
- Handling user input
Let's focus on the last one, handling user input. When the main thread is blocked, the browser can't respond to most types of user input. This can lead to a poor user experience and result in poor web performance metric scores, like Interaction to Next Paint as part of Core Web Vitals.
In the following diagram, the "Before scheduler.yield" and "After scheduler.yield" examples both take 3.5 seconds to complete. However, the "After scheduler.yield" version is able to run an important task related to user input after 1 second, while the "Before scheduler.yield" example is only able to run the important task after 3 seconds.
Understanding key web performance concepts
If you're finding terms like "main thread," "browser scheduler," and "blocking" confusing - don't worry! You're not alone.
While web performance used to focus mostly on network speed (how fast resources download), today's JavaScript-heavy web apps have shifted our attention to runtime performance (how smoothly the browser executes code).
Here's a breakdown of these important concepts:
-
Main thread: Think of this as the browser's primary worker that handles most tasks - running your JavaScript, processing user input, and painting the screen.
-
Browser scheduler: This is like a traffic controller that decides which tasks to run and when. It's responsible for ensuring that important tasks (like responding to user input) are prioritized.
-
Blocking: When a task (like a heavy JavaScript calculation) is running on the main thread, it prevents other important work from happening - like responding to a user's click.
When we talk about scheduler.yield
, we're giving that traffic controller a chance to prioritize more urgent tasks before continuing with our code.