๐ Performance - Beyond the Bundle
The fastest JavaScript is the JavaScript that never gets sent to the browser. A Programmer views every kilobyte of JS as a โperformance debtโ that costs the user CPU cycles, battery life, and data.
๐ฆ 1. Bundle Splitting & Tree Shaking
Section titled โ๐ฆ 1. Bundle Splitting & Tree ShakingโModern apps are often bloated by third-party libraries and large component trees.
- Tree Shaking: A build-step optimization (Webpack/Rollup/Vite) that removes โdead codeโ (imported functions that arenโt actually used).
- Tip: Use ES Modules (
import/export) instead of CommonJS (require) to ensure the bundler can statically analyze and prune your code.
- Tip: Use ES Modules (
- Route-Based Splitting: Only load the code needed for the current page. If a user is on the
/loginpage, they shouldnโt download the code for the/dashboard. - Vendor Splitting: Separate your frequently changing application code from rarely changing libraries (React, Lodash). This allows the browser to cache the
vendor.jsfile indefinitely.
๐ค 2. Lazy Loading & Resource Prioritization
Section titled โ๐ค 2. Lazy Loading & Resource PrioritizationโDonโt let non-essential assets block the initial render.
- Component Lazy Loading: Use
React.lazyor dynamicimport()to load heavy components (like a Data Grid or a Map) only when they are needed. - Images & Iframes: Use the native
loading="lazy"attribute. - Priority Hints: Use
<link rel="preload">for critical assets (like your main LCP image or branding font) andfetchpriority="high"to tell the browser what to grab first.
๐ง 3. The Hydration Problem
Section titled โ๐ง 3. The Hydration ProblemโHydration is the process where a static HTML page (from SSR) becomes an interactive app. It is often the โQuiet Killerโ of performance.
- The Problem: The browser has to download the JS, parse it, and โattachโ event listeners to the existing HTML. During this time, the page looks ready but is unresponsive (high Total Blocking Time).
- Progressive Hydration: Only hydrating parts of the page as they enter the viewport.
- Selective Hydration: Prioritizing the hydration of components the user is currently interacting with.
- Resumability (Qwik): A new paradigm where the serialized state is sent in the HTML, allowing the app to โresumeโ without a full hydration step.
๐ 4. Core Web Vitals (The Metrics that Matter)
Section titled โ๐ 4. Core Web Vitals (The Metrics that Matter)โ| Metric | What it measures | Optimization Goal |
|---|---|---|
| LCP (Largest Contentful Paint) | Perceived load speed. | Optimize images; use preconnect/preload; minimize render-blocking CSS. |
| INP (Interaction to Next Paint) | Responsiveness to user input. | Break up long tasks; use requestIdleCallback; optimize state updates. |
| CLS (Cumulative Layout Shift) | Visual stability. | Set explicit dimensions for images/ads; avoid inserting content above existing content. |
๐ ๏ธ Optimization Tactics
Section titled โ๐ ๏ธ Optimization Tacticsโ1. The 100KB Budget
Section titled โ1. The 100KB BudgetโSet a โPerformance Budget.โ If a PR increases the bundle size beyond a certain threshold, it requires a justification.
- Strategy: Use tools like BundleStats or Lighthouse CI in your CI/CD pipeline to catch โbundle bloatโ before it hits production.
2. Prefetching Strategies
Section titled โ2. Prefetching StrategiesโDonโt wait for the user to click.
- Speculative Prefetching: If a user hovers over a link, start prefetching the data/code for that page. (Next.js does this automatically for links in the viewport).
3. Offloading to Web Workers
Section titled โ3. Offloading to Web WorkersโJS is single-threaded. If you have to do heavy data processing (e.g., parsing a 5MB JSON), move it to a Web Worker. This keeps the main thread free to handle user input and animations.
4. Image Modernization
Section titled โ4. Image ModernizationโStop using .png or .jpg for everything.
- AVIF/WebP: These formats offer 30-50% better compression.
- Responsive Images: Use
<picture>andsrcsetto serve a 400px image to a mobile phone and a 2000px image to a 4K monitor.
๐ก Note: โFlashyโ animations and high-res videos are great for marketing, but they are the enemy of performance. Always advocate for a โPerformance-Firstโ design where the Time to Interactive is more important than the โwow factor.โ