How can a frontend perform expensive work without freezing interaction?
Break work into bounded chunks, yield to the browser, and move CPU-heavy computation to workers where appropriate.
The browser cannot respond to input while a long JavaScript task occupies the main thread. Even code that is doing useful work can make scrolling and typing feel broken if it runs for too long without yielding. Cooperative scheduling breaks work into smaller pieces so the browser gets opportunities to handle other tasks.
The browser cannot respond to input while a long JavaScript task occupies the main thread. Even code that is doing useful work can make scrolling and typing feel broken if it runs for too long without yielding. Cooperative scheduling breaks work into smaller pieces so the browser gets opportunities to handle other tasks.
The quick version
The main thread must process input, script, style, layout, and paint. Long uninterrupted tasks delay all of them. Chunk work and yield between chunks, or move isolated CPU work to a Web Worker.
Build the mental model
setTimeout yields but adds task scheduling delay. scheduler.yield() can provide a more direct cooperative yield where supported; feature-detect it and retain a fallback. Workers avoid DOM access and require message/data-transfer design.
See it in code
for (const chunk of chunks) {
process(chunk);
await new Promise((resolve) => setTimeout(resolve, 0));
}
Where it gets tricky
A worker cannot access the DOM, so it is best for pure computation or APIs designed for worker contexts. Large structured-clone messages can erase the benefit; transferable ArrayBuffers avoid copying ownership-heavy binary data. Chunking on the main thread remains useful for smaller jobs when moving the work would add more coordination than value.
A common misunderstanding
Moving work to a worker does not make serialization, copying, or coordination free.
What this changes in an application
Choose chunk sizes from responsiveness goals, use transferable buffers for large binary payloads, and handle worker errors/termination.
Questions worth exploring next
- When should you use OffscreenCanvas?
- What is the cost of structured cloning?
References and next steps
- PerformanceLongTaskTiming — primary or authoritative documentation for the underlying behavior.
- MDN Web Docs — use the relevant API reference for adjacent browser details.