MediumPerformance📖 Theory Question

What is the React Profiler and how do you use it to diagnose performance issues?

💡

Hint

React DevTools Profiler records renders — shows which component rendered, why, and how long it took

Full Answer

The React DevTools Profiler helps identify slow renders and unnecessary re-renders.

Using React DevTools Profiler:

  1. Open DevTools → Profiler tab
  2. Click Record, interact with the app, stop recording
  3. See flame chart showing each render by component and duration
  4. Click a bar to see why it rendered: props changed, hook changed, parent rendered

The Profiler API for automated performance testing:

import { Profiler } from 'react';

function onRenderCallback(
  id,           // tree id (prop)
  phase,        // "mount" | "update" | "nested-update"
  actualDuration, // time spent rendering committed update
  baseDuration,   // estimated time without memoization
  startTime,
  commitTime
) {
  if (actualDuration > 16) { // over one 60fps frame
    console.warn(`Slow render in ${id}: ${actualDuration}ms`);
  }
}

function App() {
  return (
    
      
    
  );
}

What to look for:

  • Components that render when they shouldn't (add React.memo)
  • baseDuration >> actualDuration → memoization is working
  • actualDuration consistently high → expensive render, consider useMemo
💡 Profile before you optimize. Adding useMemo/useCallback everywhere without profiling first adds complexity and can actually slow things down by running extra comparisons.

More Performance Questions

EasyWhat is React.memo and when does it actually prevent re-renders?EasyWhat is code splitting and lazy loading in React?MediumWhat is virtualization (windowing) and when do you need it?MediumWhat causes unnecessary re-renders and how do you diagnose them?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint