HardConcept

How do you optimize performance in a React application?

Technology:
React
Experience:
Senior

Quick answer

Measure first with the Profiler and Lighthouse, then reduce unnecessary renders, split code with lazy loading, virtualise long lists, and optimise images and bundle size.

Detailed explanation

Rendering: avoid wasted re-renders by colocating state, using React.memo, useMemo and useCallback where profiling shows a benefit, and avoiding creating new objects in props for memoised children. For slow updates triggered by typing or filtering, useTransition and useDeferredValue keep the interface responsive.

Loading: split the bundle by route with React.lazy and Suspense (or Next.js dynamic imports), remove unused dependencies, and analyse the bundle with a tool such as webpack-bundle-analyzer. Serve properly sized, modern-format images and lazy-load below-the-fold content.

Data and lists: virtualise long lists with a library such as react-window so only visible rows are in the DOM, cache server data with React Query or SWR, and debounce expensive handlers. Track real-user metrics such as Largest Contentful Paint and Interaction to Next Paint, not just lab scores.

Example

jsx
const Reports = React.lazy(() => import("./Reports"));

<Suspense fallback={<Spinner />}>
  <Reports />
</Suspense>

Key points

  • Profile before optimising
  • Reduce renders, reduce bundle size, reduce DOM size
  • Use real-user metrics to verify improvements
  • ReactMedium

    What is the difference between SSR, SSG and CSR in React?

    CSR renders in the browser after JavaScript loads, SSR renders the HTML on the server for each request, and SSG renders the HTML once at build time; SSR and SSG give crawlers and users content faster, which helps SEO.

    Mid-level 路 Senior 路 Performance
  • ReactEasy

    What is the virtual DOM in React and how does it work?

    The virtual DOM is a lightweight JavaScript copy of the UI that React compares with the previous version after each update, so it only changes the parts of the real DOM that actually differ.

    Intern 路 Junior 路 Performance
  • ReactEasy

    Why do we need keys in React lists?

    Keys give each list item a stable identity so React can match items between renders and correctly add, remove or reorder them without recreating or mixing up their state.

    Junior 路 Mid-level 路 Performance
  • ReactMedium

    What is the difference between useRef and useState in React?

    Updating state triggers a re-render, while changing a ref's .current value does not; use state for data shown on screen and refs for DOM access or values that must persist without causing renders.

    Mid-level 路 Senior 路 React Hooks