Code Splitting: Don't Load What the User Can't See Yet
Most React apps load the code for every page all at once. The login page loads code for the dashboard, course viewer, settings, and admin panel — even though the user doesn't need any of it yet. The fix: React.lazy() with Suspense.
- Use React.lazy() for every major route: const Dashboard = lazy(() => import('./Dashboard'))
- Add Suspense with a lightweight fallback — a simple spinner beats a white screen
- Vite does route-level Code Splitting automatically — use it before anything else
- Heavy video player? Load it lazily only when the user actually starts playing
- Real result: we reduced initial bundle size by 67% with Code Splitting alone
Preventing Re-renders: React.memo, useMemo, and useCallback
The most common mistake: adding React.memo to everything. This actually slows your app down because the comparison itself has a cost. React.memo is useful only when:
- The component re-renders frequently even though its props haven't changed
- The component is computationally expensive: large data tables, complex charts
- The component appears in a long list of similar items (100+ items)
- useMemo: for heavy calculations only — filtering 10,000 records, aggregating statistics
- useCallback: for event handlers passed to memoized child components
Virtual Lists: 1,000+ Items Without a Slowdown
Teacher admin panel showing 800 students? Course list with 300 units? Rendering every element in the DOM kills memory and freezes the browser. Virtualization renders only what's currently visible on screen — the rest is computed on demand.
- @tanstack/react-virtual: lightest and most flexible — only 6KB
- react-window: simpler API for straightforward list cases
- Without virtualization: rendering 1,000 rows = 200ms + massive RAM consumption
- With virtualization: rendering 20 visible rows = 4ms
- Rule of thumb: add virtualization to any list that exceeds 50-100 items
Image Optimization: WebP and Lazy Loading
Course thumbnails and profile photos are a major source of page bloat. On an average educational platform, images account for 60-70% of page weight.
- Convert all images to WebP — 25-30% smaller than JPEG at the same quality
- Add loading='lazy' to every image not in the initial viewport
- Use srcset for responsive images — don't send a 2000px image to a 400px screen
- Blur placeholder with base64 improves UX while large images load
- Cloudinary or imgix for automatic conversion per device — worth the subscription
Bundle Analysis: Find the Hidden Weight
Before any optimization, understand what's in your bundle. A 200KB charting library for one simple graph? moment.js at 67KB to format a single date? These exist in most real projects.
- Vite: use rollup-plugin-visualizer for a visual bundle map
- Replace moment.js with day.js — same functionality at 2KB instead of 67KB
- lodash imports: use import { debounce } from 'lodash-es', not the full package
- Icon libraries: import individual icons, not the entire icon set
- Check tree shaking support before adopting any new library
Measuring Real-World Performance: Core Web Vitals
Lighthouse numbers are useful but don't always reflect real user experience. Use these tools to measure LCP, CLS, and INP from actual browser sessions:
- web-vitals (Google): 2KB, measures real browser performance and sends to analytics
- Sentry Performance: automatically collects data from real users with full context
- Google Search Console: Core Web Vitals from your actual users — more important than Lighthouse
- Good LCP: < 2.5s | Good CLS: < 0.1 | Good INP: < 200ms
| Metric | Good | Needs Work | Poor |
|---|---|---|---|
| LCP (Largest Contentful Paint) | < 2.5s | 2.5s – 4s | > 4s |
| INP (Interaction to Next Paint) | < 200ms | 200ms – 500ms | > 500ms |
| CLS (Cumulative Layout Shift) | < 0.1 | 0.1 – 0.25 | > 0.25 |
