Measure first with the React DevTools Profiler, Chrome Performance panel and Lighthouse, then fix the actual bottleneck. Reduce unnecessary re-renders - Keep state as local as possible; lifting state too high re-renders large trees. - React.memo for components that receive the same props often, combined with stable props (useCallback and useMemo for callbacks and objects passed down). - Split large contexts, and memoise context values, so updates do not re-render every consumer. - Use stable, unique keys in lists (never array indexes for reorderable lists). - The React Compiler can automatically memoise components and values in supported setups. Make expensive work cheaper - useMemo for expensive calculations (filtering or sorting large lists). - useTransition and useDeferredValue to keep typing responsive while heavy updates render. - Move heavy computation to Web Workers. Large lists and tables - Virtualise long lists (TanStack Virtual, react-window) so only visible rows are rendered. - Paginate or use infinite scroll with server-side filtering. Loading performance - Code splitting with React.lazy and route-based lazy loading; dynamic import of heavy libraries (charts, editors). - Analyse bundles (rollup-plugin-visualizer for Vite, webpack-bundle-analyzer) and remove or replace large dependencies. - Optimise images: modern formats (WebP or AVIF), correct sizes, loading="lazy" for off-screen images, and priority for the main LCP image. - Serve from a CDN with compression and long cache headers for hashed assets. - Consider server-side rendering or static generation (Next.js, React Router framework mode) for content-heavy or SEO-critical pages, and React Server Components where available. Data fetching - Use TanStack Query or RTK Query for caching, deduplication and background refetching. - Avoid request waterfalls: fetch in parallel and start fetching early (route loaders). - Debounce search inputs and cancel outdated requests. Avoid common anti-patterns - Creating components inside other components (they remount every render). - Storing derived data in state and syncing with useEffect. - Huge component trees updating on every keystroke from a global store. Track Core Web Vitals (LCP, INP, CLS) in production and set performance budgets in CI.
Ravindra Wadile · · 0 likes