Use Chrome DevTools Performance tab to record and analyze flame charts, identify long tasks, and find layout thrashing. Common fixes include breaking up long tasks, avoiding forced synchronous layouts, using requestAnimationFrame, and moving work to Web Workers.
Performance Analysis:
Common Issues:
Solutions:
scheduler.yield()// BAD: Long blocking task
function processLargeArray(items) {
for (let i = 0; i < items.length; i++) {
heavyComputation(items[i]); // Blocks main thread
}
}
// GOOD: Chunked processing with yielding
async function processLargeArrayAsync(items, chunkSize = 100) {
for (let i = 0; i < items.length; i += chunkSize) {
const chunk = items.slice(i, i + chunkSize);
chunk.forEach(item => heavyComputation(item));
// Yield to browser - allow event handling, rendering
await new Promise(resolve => setTimeout(resolve, 0));
}
}
// Using scheduler.yield() (if available)
async function processWithYield(items) {
for (const item of items) {
heavyComputation(item);
// Yield if task is taking too long
if (scheduler.yield) {
await scheduler.yield();
}
}
}
// Using requestIdleCallback for non-urgent work
function processInBackground(items) {
let index = 0;
function processChunk(deadline) {
while (index < items.length && deadline.timeRemaining() > 0) {
heavyComputation(items[index]);
index++;
}
if (index < items.length) {
requestIdleCallback(processChunk);
}
}
requestIdleCallback(processChunk);
}