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
1 1. DOM Construction (HTML ──► DOM Tree)
2 │
3 2. CSSOM Construction (CSS ──► CSS Object Model)
4 │
5 ▼
6 3. Render Tree (Combines visible DOM nodes with matched CSS rules)
7 │
8 ▼
9 4. Layout (Reflow) (Computes exact pixel geometry, coordinates, and size of every box)
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:
1// DANGER: Layout Thrashing (Forces 100 Reflows in a single frame!)