Frontend rendering optimization, code splitting, memoization strategies, bundle size control, asset optimization, and memory management. Use when optimizing component rendering, reducing bundle size, debugging performance issues, implementing lazy loading, or reviewing code for performance regressions.
Provides optimization strategies for frontend rendering performance, bundle size reduction, and memory management to maintain fast, responsive user interfaces.
<principles> <measure-before-optimizing> Never optimize without data. Profile first using browser devtools, bundle analysis tools, or runtime performance APIs. Premature optimization adds complexity without proven benefit. Identify the actual bottleneck before writing optimization code. </measure-before-optimizing> <less-is-more> The most effective optimization is shipping less code. Lazy-load routes and heavy components. Tree-shake unused exports. Avoid pulling in large libraries for small tasks. Every kilobyte costs users time. </less-is-more> <memoization-is-not-free> Component memoization, computed value caching, and callback stabilization have overhead — memory for cached values and comparison cost on every render. Only memoize when: the computation is expensive, the component re-renders frequently with the same props, or reference stability matters for downstream consumers. Over-memoizing is a code smell. </memoization-is-not-free> </principles>Use the checklist below and track your progress:
Progress:
- [ ] Step 1: Measure current performance
- [ ] Step 2: Optimize bundle size
- [ ] Step 3: Optimize rendering
- [ ] Step 4: Optimize assets and DOM
- [ ] Step 5: Manage memory
- [ ] Step 6: Verify improvement
Step 1: Measure current performance
Before optimizing anything, establish baselines:
performance.mark() / performance.measure() for custom timing of specific operations.Step 2: Optimize bundle size
Reduce what ships to the user:
import { Button } from 'lib' — never import * as Lib from 'lib'. Wildcard imports defeat tree shaking and pull in the entire module.index.ts) that re-export everything from a folder can prevent tree shaking. For large libraries, prefer direct file imports (e.g., import { Button } from 'lib/components/Button') over barrel imports.depcheck or equivalent) regularly. Remove unused packages. Before adding a new dependency, check its bundle impact — a small utility should not cost 50KB.Step 3: Optimize rendering
Reduce unnecessary re-render work:
Step 4: Optimize assets and DOM
width and height attributes to prevent layout shift. Lazy-load images below the fold using native loading="lazy" or an intersection observer.transform and opacity for animations (GPU-composited) instead of top, left, width, or height (trigger layout recalculation). Use will-change sparingly and only on elements that are about to animate — leaving it on permanently increases memory usage.font-display: swap or font-display: optional to prevent invisible text during font load. Preload critical fonts. Subset fonts to include only the characters actually used.async or defer to prevent blocking the main thread. async downloads and executes as soon as ready (non-blocking, out of order). defer executes after HTML parsing in document order. For critical third-party origins, add <link rel="preconnect"> to reduce connection setup time. Lazy-load non-essential third-party resources (video embeds, social widgets) until the user scrolls to them or interacts. Audit third-party script count — set a resource budget (e.g., max 10 third-party requests) and enforce it.Step 5: Manage memory
Prevent leaks that degrade performance over time:
addEventListener must have a corresponding removeEventListener in the component's cleanup phase. Forgetting cleanup causes listeners to accumulate on long-lived pages.setTimeout needs clearTimeout and every setInterval needs clearInterval in cleanup. Leaked intervals continue executing after the component is gone.AbortController to cancel pending network requests when the component unmounts. Responses arriving after unmount can cause state updates on unmounted components.Step 6: Verify improvement
After applying optimizations, close the loop:
Bundle size: Run the project's build command and bundle analyzer. Compare chunk sizes against the baselines from Step 1. Flag any chunk still over 200KB gzipped.
Lighthouse audit: Run a Lighthouse audit against the application. Use vscode/askQuestions to gather two inputs from the user:
Default thresholds (based on web.dev/Google recommendations):
| Metric | Good | Needs Improvement | Poor |
|---|---|---|---|
| Lighthouse Performance Score | ≥ 90 | 50–89 | < 50 |
| LCP (Largest Contentful Paint) | ≤ 2.5s | 2.5s–4.0s | > 4.0s |
| INP (Interaction to Next Paint) | ≤ 200ms | 200ms–500ms | > 500ms |
| CLS (Cumulative Layout Shift) | ≤ 0.1 | 0.1–0.25 | > 0.25 |
Execution steps:
package.json).npx lighthouse <URL> --output=json --output-path=./lighthouse-report.json --chrome-flags="--headless".Quantify the delta: Every claimed improvement needs a number — e.g., "main chunk reduced from 280KB to 140KB gzipped", "LCP improved from 3.2s to 1.8s". If no measurable improvement, the optimization was either targeting the wrong bottleneck or introduced regression elsewhere — revert and re-profile.
Document results: Record post-optimization metrics alongside the baselines from Step 1 for future reference.
| Situation | Memoize? | Why |
|---|---|---|
| Expensive computation (sort/filter 1000+ items) | Yes (cache computation) | Saves CPU on re-renders |
| Handler passed to memoized child | Yes (stabilize reference) | Preserves referential equality |
| Simple arithmetic or string concat | No | Memo overhead exceeds computation cost |
| Component with static or rarely-changing props | Maybe (component memo) | Profile first — often unnecessary |
| Inline object passed as prop | Yes (lift or cache) | New identity each render causes child re-render |
| Top-level handler (not passed down) | No | Nothing benefits from a stable reference |
| Red Flag | Action |
|---|---|
| Single chunk over 200KB (gzipped) | Code split — lazy-load routes or heavy features |
Heavy date library (e.g., moment.js) | Replace with lightweight alternative (date-fns, dayjs) |
Wildcard import (import * as) | Switch to named imports for tree shaking |
| Barrel re-exports pulling unused code | Import directly from source file |
Unused dependencies in package.json | Run audit tool and remove unused |
| CSS framework loaded fully (not purged) | Enable CSS purging in build configuration |
| Polyfills for widely-supported features | Remove or conditionally load based on browser targets |
Performance:
- [ ] Bundle analyzed — no unexpected large chunks
- [ ] Routes lazy-loaded with loading boundaries/fallbacks
- [ ] Heavy components (modals, charts) loaded on demand
- [ ] Rendering strategy evaluated (SSR/SSG where applicable)
- [ ] Named imports only — no wildcard imports
- [ ] Memoization/caching applied where measured benefit exists
- [ ] No object/array literals created in render path
- [ ] State subscriptions are selective (not full-store)
- [ ] Images optimized (modern format, width/height, lazy-load)
- [ ] Animations use transform/opacity (not layout properties)
- [ ] All effects clean up: timers, listeners, abort controllers
- [ ] Long lists virtualized when item count exceeds visible area
- [ ] Third-party scripts loaded async/defer, non-essential ones lazy-loaded
- [ ] Baseline metrics documented for comparison
- [ ] Post-optimization metrics compared against baselines
| Anti-Pattern | Instead Do |
|---|---|
| Optimizing without profiling | Measure first, optimize second |
| Memoizing every component | Profile; only memoize components with frequent re-renders and stable props |
| Caching trivial computation | Skip — overhead exceeds benefit |
| Unstable handler reference passed to memoized child | Stabilize the handler reference to preserve referential equality |
Inline style={{}} objects | Lift to module scope or memoize |
| Loading entire library synchronously | Code-split and lazy-load heavy modules |
| Effect without cleanup | Always return a cleanup function for subscriptions and timers |
Over-relying on will-change | Use sparingly — permanent use increases memory consumption |
| Using array index as list key | Use stable unique identifiers (database IDs) |
| Global state for component-local concerns | Keep state close to where it is consumed |
The patterns above are framework-agnostic. For framework-specific optimization APIs, load the appropriate reference:
./references/react-patterns.md — React.memo, useMemo, useCallback, React.lazy, Suspense.implementing-frontend — for component patterns that support performant renderingwriting-hooks — for memoization and cleanup patterns within custom hooks