Critical Rendering Path & Reflow/Repaint
Critical Rendering Path & Reflow/Repaint in Modern JavaScript
Understanding how browsers convert HTML, CSS, and JavaScript into pixels on the display screen is the hallmark of senior frontend engineers. The Critical Rendering Path (CRP) dictates rendering performance, and careless DOM manipulation triggers devastating Reflows (Layout) and Repaints.
1. The 5 Steps of the Critical Rendering Path
2. Reflow vs. Repaint
| Operation | Triggered By | Cost | Description |
|---|---|---|---|
| Reflow (Layout) | Changes affecting geometry, dimensions, or positions (width, height, margin, font-size, adding/removing nodes) | Extremely Expensive | Recomputes layout geometry of target node and all affected ancestors/descendants! |
| Repaint (Paint) | Changes affecting appearance only without changing dimensions (color, background-color, box-shadow, visibility) | Moderate | Redraws pixels on affected layers without recalculating layout coordinates. |
| Composite Only | Changes using transform and opacity | Cheapest (Ideal) | GPU composites layers directly with zero CPU reflow and zero CPU repaint! |
3. The Performance Killer: Forced Synchronous Layout (Layout Thrashing)
Normally, browsers batch DOM mutations and execute Reflow asynchronously at the end of the frame. However, if code reads geometric properties immediately after writing styles, the browser is forced to flush layout synchronously:
Properties that Trigger Forced Reflow:
element.offsetWidth,offsetHeight,clientWidth,clientHeightelement.getBoundingClientRect()window.scrollY,window.getComputedStyle()
The Fix: Batch Reads First, Writes Second
4. Hardware Acceleration with CSS will-change
Promoting an element to its own GPU compositor layer bypasses CPU painting:
Practice Quiz
requestAnimationFrame & Smooth Animations
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Streaming JSON & NDJSON | requestAnimationFrame & Smooth Animations |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.