Skip to main content

Command Palette

Search for a command to run...

3) Performance Measurement

Updated
View as Markdown
  1. What “performance” means in React
    Measuring performance in a React app isn’t one single metric—it’s a combination of render performance, load time, runtime behavior, and user experience. The key is to measure from multiple angles and then correlate the results.
    You’re typically measuring:
    ⏱️ Initial load time (how fast app loads)
    ⚛️ Render performance (how fast components render)
    🔁 Re-render frequency (unnecessary updates)
    🎯 User experience metrics (interaction speed, responsiveness)
    🌐 Network performance (API calls, bundle size)

  2. How to Measure Performance
    i) Component-Level Profiling
    Focus: React rendering behavior
    Measure: Render time, Re-render count, Render causes
    Best for debugging UI lag
    ii) Browser-Level Profiling
    Focus: overall app performance
    CPU usage
    Memory usage
    FPS (frames per second)
    JS execution time
    iii) Real User Metrics (RUM)
    Focus: real-world performance
    Page load speed
    Interaction delays
    Network latency
    iv) Bundle Size Analysis
    Focus: how heavy your app is
    Large bundle = slow load time
    v) Synthetic Testing
    Focus: lab testing under controlled conditions
    Simulate slow network/devices

  3. React-Specific Tools
    i) React DevTools - Most important tool for React developers.
    Features -
    Component tree inspection
    Profiler (render time + re-renders)
    “Why did this render?”
    Use for -
    Debugging re-renders
    Component optimization
    ii) React Profiler API
    Built-in API for programmatic profiling

    <Profiler id="App" onRender={callback}>
      <App />
    </Profiler>
    

    Use for: Logging performance metrics

  4. Browser Tools
    i) Google Chrome DevTools
    Key tabs:
    Performance tab - Record runtime performance and Analyze CPU usage, FPS
    Memory tab - Detect memory leaks
    Network tab - API timing, payload size
    Use for - Deep performance debugging
    ii) Lighthouse (inside Chrome DevTools)
    Measures: Performance score, Accessibility, Best practices
    Metrics: LCP (Largest Contentful Paint), CLS (Cumulative Layout Shift), TTI (Time to Interactive)

  5. Real User Monitoring (RUM Tools)
    i) Google Analytics/Adobe Analytics
    Tracks user load times
    Page performance
    ii) Sentry
    Tracks slow transactions
    Monitors real user performance
    iii) New Relic
    Full-stack performance insights

  6. Bundle Analysis Tools
    i) Webpack Bundle Analyzer
    Visualizes bundle size
    Helps: remove unused code Optimize imports

  7. Synthetic Testing Tools
    i) Lighthouse
    Simulates slow devices/networks

  8. Performance Optimization Checklist
    i) Define Performance Budgets (Before Coding)
    Set targets so regressions are obvious:
    LCP < 2.5s
    TTI < 3s
    Bundle size < 200–300 KB (initial)
    API response < 300 ms
    FPS ≥ 55–60
    Use Lighthouse to validate.
    ii) React Rendering Optimization
    Avoid unnecessary re-renders
    - Use React.memo for pure components
    - Use useMemo for expensive calculations
    - Use useCallback for stable function references
    Keep state minimal & local
    - Don’t lift state unnecessarily
    - Split large components
    Optimize context usage
    - Avoid global re-renders
    - Split contexts by domain
    iii) Profiling & Debugging
    Use:
    React DevTools → component profiling
    Google Chrome DevTools → CPU/memory
    iv) Bundle Size Optimization
    Reduce bundle size
    - Code splitting (React.lazy, dynamic imports)
    - Tree shaking (ES modules)
    - Remove unused dependencies
    Analyze bundle
    - Use Webpack Bundle Analyzer
    - Remove large libraries (moment.js → date-fns)
    v) Network Optimization
    API efficiency
    - Use caching (HTTP cache, SWR, React Query)
    - Debounce/throttle API calls
    - Batch requests
    Asset optimization
    - Compress images (WebP/AVIF)
    - Use CDN
    - Enable gzip/brotli
    vi) Rendering Strategy
    Use modern patterns
    - Lazy loading (routes/components)
    - Server Side Rendering/SSG (for SEO + faster load)
    vii) Memory Optimization
    Prevent memory leaks
    - Clean up useEffect
    - Remove event listeners
    - Cancel API calls
    viii) Production Build Optimization
    Ensure:
    - Minified code
    - Dead code elimination
    - Environment variables optimized
    ix) Monitoring & Real User Metrics
    Track real users
    - Google Analytics
    - Sentry
    I optimize React apps by profiling with React DevTools, reducing re-renders via memoization, optimizing bundle size with code splitting, improving network efficiency, and validating improvements using Lighthouse and real user metrics.