Course contentsShow
← React
Lesson 2262 of 2,39649. Performance: Identifying ProblemsFree lesson

Long Tasks and Main Thread Blocking

Use PerformanceObserver to detect long tasks that freeze the UI and identify React render bottlenecks.

Long Tasks and Main Thread Blocking

What you'll learn: How to use PerformanceObserver to detect long tasks that freeze your UI and pinpoint React render bottlenecks.

What Are Long Tasks?

The browser's main thread handles everything: JavaScript execution, rendering, user input. When a single task takes longer than 50ms, it's considered a "long task." During that time, the UI is completely frozen—clicks don't register, animations stutter, the page feels broken.

In React apps, long tasks often come from expensive renders: deeply nested components, heavy computations in render functions, or cascading re-renders that block the main thread.

Analogy: Think of the main thread as a single checkout lane at a grocery store. If one customer takes 10 minutes to pay, everyone behind them waits. Long tasks are that slow customer—they block everything else.

Detecting Long Tasks with PerformanceObserver

The PerformanceObserver API can monitor long tasks automatically:

const observer = new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    console.warn('Long task detected:', {
      duration: entry.duration,
      startTime: entry.startTime,
      name: entry.name
    });
  }
});

observer.observe({ entryTypes: ['longtask'] });

This logs every task exceeding 50ms. In production, you'd send this data to your analytics instead of console.warn.

Identifying React Render Bottlenecks

When a long task fires, check the React DevTools Profiler recording around that timestamp. Look for:

  • Components with long render durations
  • Many components rendering simultaneously
  • Cascading renders caused by state updates

Combine PerformanceObserver data with Profiler evidence to trace freezes back to specific components or state changes.

Key Takeaway: Use PerformanceObserver to detect when your app blocks the main thread for >50ms, then correlate those long tasks with React Profiler data to find which renders are causing the freeze.