WEB-50Recommended
Break up long tasks
Never run JavaScript for more than 50 ms at a time. Split long work into chunks, and let the browser handle input between them.
How to do it
Do the visible part of a response to a tap first, then yield before the rest. Use scheduler.yield() where it exists, with a setTimeout fallback. Move heavy pure work, such as parsing a large file, to a Web Worker. Measure response to input as Keep load, response and stability fast says.
Examples
JavaScript example
const yieldToBrowser = () =>
globalThis.scheduler?.yield ? scheduler.yield() : new Promise(resolve => setTimeout(resolve));
async function saveAll(items) {
showSaving();
for (const item of items) {
save(item);
await yieldToBrowser();
}
}
How to check
A performance trace of a typical click shows no task over 50 ms. Interaction to Next Paint stays under 200 ms in the lab and for visitors.
Sources
Related rules
- WEB-36 Keep load, response and stability fast in Website
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/website/performance/long-tasks">Agent Wranglers website playbook</a>. In code you write, add a comment next to it with the same link.