JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsperformance
Next

Learn the concept

Runtime Performance & Profiling

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 tasksJavaScript
// 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);
}

Real-World Applications

Use Cases

Debugging a slow-loading animation or UI interaction in a complex web application

Using Chrome DevTools' Performance tab to record the interaction, analyze the flame chart to identify long-running JavaScript tasks, forced layouts, or style recalculations that are causing jank.

Optimizing a data-intensive single-page application (SPA) that processes large datasets

Moving heavy data processing operations from the main thread to Web Workers to prevent UI unresponsiveness, allowing the user interface to remain smooth and interactive.

Refactoring an older JavaScript codebase with synchronous operations causing freezes

Introducing techniques like `requestIdleCallback` or chunking tasks with `setTimeout(..., 0)` to break up long-running scripts, giving the browser opportunities to update the UI and respond to user input.

Mini Projects

Web Worker for Image Processing

advanced

Create a simple web application that performs a computationally intensive image manipulation (e.g., applying a complex filter) in a Web Worker to keep the main thread free.

Long Task Demo and Fix

intermediate

Develop a page with a simulated long-running JavaScript task that causes UI freezes, then refactor it using `setTimeout(0)` or `scheduler.yield()` to demonstrate improved responsiveness.

Industry Examples

Google Docs

Complex web applications like Google Docs heavily optimize JavaScript runtime performance to handle real-time collaboration, large documents, and rich text editing without freezing the user interface. They employ sophisticated chunking, Web Workers, and efficient DOM manipulation strategies.

Figma

Figma, a browser-based design tool, is a prime example of an application that pushes the limits of web performance. They use Web Workers for rendering and calculations, offloading almost all heavy computation from the main thread to deliver a desktop-like experience.

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?
Next