๐ Optimization - Rendering & Profiling
Smoothness is just as important as speed. A site that loads in 1 second but freezes for 500ms when you click a button is a failure. We optimize for the RAIL model: Response, Animation, Idle, and Load.
๐๏ธ 1. Understanding the Critical Rendering Path (CRP)
Section titled โ๐๏ธ 1. Understanding the Critical Rendering Path (CRP)โTo optimize rendering, you must understand the steps the browser takes to turn code into pixels.
- DOM & CSSOM: The browser parses HTML into the DOM and CSS into the CSSOM.
- Render Tree: Combining the two to determine what is visible.
- Layout (Reflow): Calculating the exact geometry (width, height, position) of each element.
- Paint: Filling in the pixels (colors, shadows, text).
- Composite: Layering elements together to draw the final frame.
- Programmerโs Move: Avoid properties that trigger Layout (like
width,top,margin) during animations. Use properties that only trigger Composite (liketransformandopacity) because they are handled by the GPU, not the CPU.
โก 2. Reactivity & Render Cycles
Section titled โโก 2. Reactivity & Render CyclesโFrameworks like React or Vue can sometimes render too much or too often.
Reconciliation Bottlenecks
Section titled โReconciliation Bottlenecksโ- The Problem: A state change in a parent component causes a massive re-render of 100 children, even if their props didnโt change.
- The Fix:
- React: Use
React.memo,useMemo, anduseCallbackto prevent unnecessary updates. - Vue: Use
v-oncefor static content orshallowReffor large objects that donโt need deep reactivity. - General: Lift state down. Move state as close to the leaf components as possible to contain the โrender blast radius.โ
- React: Use
โฑ๏ธ 3. Profiling in the Browser
Section titled โโฑ๏ธ 3. Profiling in the BrowserโStop guessing why your app is slow. Use the Chrome DevTools Performance Tab.
How to Profile like a Pro:
Section titled โHow to Profile like a Pro:โ- Record a Trace: Perform the slow action (e.g., opening a modal).
- Identify โLong Tasksโ: Look for red flags on the main thread timeline. Any task over 50ms is a โLong Taskโ that can cause input lag.
- The Flame Graph: Drill down into the stack trace to find the specific function call or framework internal that is hogging the CPU.
๐ ๏ธ 4. Advanced Optimization Tactics
Section titled โ๐ ๏ธ 4. Advanced Optimization TacticsโWindowing / Virtualization
Section titled โWindowing / VirtualizationโIf you have a list of 10,000 items, donโt render 10,000 DOM nodes.
- Strategy: Use libraries like
react-windoworvue-virtual-scroller. These only render the items currently visible in the viewport, reducing memory usage and layout time by 99%.
Debouncing & Throttling
Section titled โDebouncing & ThrottlingโFor high-frequency events like scroll, resize, or keyup (for search):
- Debounce: Wait until the user stops typing before firing the search API.
- Throttle: Limit the execution to once every X milliseconds (e.g., updating a progress bar during a scroll).
Offloading with Web Workers
Section titled โOffloading with Web WorkersโThe โMain Threadโ handles JS, Layout, and Paint. If you perform a heavy calculation there, the UI freezes.
- Programmerโs Move: Move non-UI-blocking tasks (data processing, complex sorting, image manipulation) to a Web Worker. It runs on a separate thread and communicates with the main thread via messages.
โ๏ธ The Performance Budget
Section titled โโ๏ธ The Performance BudgetโA Programmer sets hard limits to prevent โperformance creep.โ
- JavaScript Limit: e.g., โMain thread execution must stay under 300ms on a 4x slowed-down CPU (mobile simulation).โ
- Lighthouse Scores: Automate Lighthouse checks in CI/CD to ensure LCP (Largest Contentful Paint) stays under 2.5s.
๐ก Note: Donโt optimize for the M3 Macbook Pro you are developing on. Use the CPU Throttling and Network Throttling features in DevTools to see how your app feels on a $200 Android phone on a 3G network. That is where the real performance bugs live.