JavaScript · Senior · Question 27 of 75
How do you avoid blocking the UI when processing 100,000 items on the main thread?
- ✓Correct answer: Split the work into chunks and yield the thread (or move it to a worker)
- 2Wrap the loop in an
asyncfunction - it runs in a background thread - 3Use
Promise.all- promises run in parallel threads
Explanation
Tasks longer than 50 ms hurt responsiveness (INP). async doesn't create a new thread: a synchronous loop inside still blocks. Cut the work into pieces and yield via setTimeout, scheduler.yield() (where supported) or requestIdleCallback; move heavy computation to a Web Worker.