JS Guide
HomeQuestionsSearchResources
Search

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsperformance
Next
performance
senior
runtime

How do you identify and fix JavaScript runtime performance issues?

runtime-performance
profiling
web-workers
layout-thrashing
Quick Answer

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.

Detailed Explanation

Performance Analysis:

  • Chrome DevTools Performance panel
  • Flame charts show call stack over time
  • Long Tasks blocking main thread
  • Layout/style recalculations

Common Issues:

  1. Long Tasks: >50ms blocking tasks
  2. Layout Thrashing: Read/write cycles
  3. Memory Leaks: Growing heap
  4. Excessive GC: Frequent pauses

Solutions:

  • Break long tasks with scheduler.yield()
  • Batch DOM reads/writes
  • Use Web Workers for heavy computation
  • Virtualize long lists
  • Debounce/throttle handlers

Code Examples

Breaking up long tasks
// 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);
}

Resources

Chrome DevTools Performance

docs

Web.dev - Long Tasks

article

Related Questions

How do you identify and fix memory leaks in JavaScript applications?

senior
memory
Next
How do you identify and fix memory leaks in JavaScript applications?