Saturday, November 9, 2024

React optimization In quick high level summary

 

  1. Batch state updates to minimize re-renders
    React automatically batches state updates in event handlers, but outside of these (e.g., in promises or timeouts), multiple state updates cause unnecessary re-renders. Use unstable_batchedUpdates from React DOM to manually batch updates outside of event handlers.
  2. Use React.PureComponent for automatic shallow prop checks
    If your components only depend on shallow prop comparisons, use React.PureComponent instead of React.Component to avoid unnecessary re-renders. This is an easier alternative to manually using shouldComponentUpdate for simple cases.
  3. Use dynamic imports for large libraries
    For large libraries such as lodash, import only the functions you need. Instead of import _ from 'lodash', use import debounce from 'lodash/debounce' to avoid loading the entire library unnecessarily, reducing bundle size.
  4. Throttle or debounce expensive operations
    If you have functions that trigger on frequent user interactions (e.g., search bars, scrolling), use throttling or debouncing to limit how often they execute. Libraries like lodash.debounce can help prevent performance bottlenecks in components.
  5. Leverage React’s useCallback for stable function references
    Functions in React components are re-created on every render, which can cause unnecessary re-renders in child components that receive these functions as props. Use useCallback to memoize functions and pass stable references to child components.
  6. Use Suspense with fallback components for seamless loading
    To improve the user experience during lazy loading, make use of Suspense with well-designed fallback components. The fallback should be lightweight and informative to keep users engaged during loading times.
  7. Prerender critical pages for faster time-to-interactive
    Use prerendering techniques (e.g., Next.js’s Static Site Generation or Server-side Rendering) to precompute HTML for critical pages. This reduces the time-to-interactive for first-time visitors and improves overall page load performance.
  8. Keep component trees shallow
    Deeply nested component trees can slow down rendering. Keep the component hierarchy as flat as possible by breaking large components into smaller, reusable pieces and using React’s composition model to simplify the tree.
  9. Profile performance using React Developer Tools
    Use React’s Profiler to analyze components that are re-rendering unnecessarily. Identify “hot” components that are causing performance degradation, and address these by applying memoization or splitting components.
  10. Reduce third-party dependencies in production
    Periodically audit and remove unnecessary third-party libraries, especially those included during development but not used in production. Bundle size tools like Webpack Bundle Analyzer can help identify large dependencies, helping you decide whether to remove or replace them.

No comments:

Post a Comment

Performance optimization techniques in ReactJS

Summary: Helps to learn how to measure performance improvements. As the majority of modern web applications rely on what React.js brings to ...