EN/CH Mode
BRUCE_FE Interview Notes - Page Loading - Explaining Web Rendering Process (CRP Path)
In-depth analysis of the browser's Critical Rendering Path (CRP), including DOM, CSSOM, render tree construction, and optimization strategies for reflow and repaint, with complete answers to common interview questions.
EN/CH Mode

Lazy to read articles? Then watch videos!
Basic Concepts
The process of converting HTML, CSS, and JavaScript into pixels is called the Critical Rendering Path (CRP). Understanding this process is crucial for optimizing web performance.
User requests URL ex: https://www.google.com
โ
โ
Server processes request โโโโโโโโโโโโโโโโโโโโโโ
โ โ
โโโ SSR (Server-Side Rendering) โ
โ Server generates complete HTML โ
โ โ
โโโ SSG (Static Site Generation) โ
โ Returns pre-generated static HTML โ
โ โ
โโโ SPA (Single Page Application) โ
Returns basic HTML framework โ
โ โ
โ โ
HTML returns to browser โโโโโโโโโโโโโโโโโโโโโโโ
โ
โ
HTML โโโโโโโ
โ
DOM Tree CSS โโโโโโโ
โ โ
โ CSSOM Tree
โ โ
โโโโโโโโโโโฌโโโโโโโโโโ
โ
Render Tree โโโโ JavaScript Execution
โ (can modify DOM and CSSOM)
โ
Layout
โ
โ
Paint
โ
โ
Composite1. HTML Parsing โ DOM Tree
The browser converts HTML into the DOM (Document Object Model), which serves as the object representation of the webpage and interface for JavaScript operations. The parsing process is progressive, allowing for incremental content processing.
2. CSS Parsing โ CSSOM Tree
CSS is parsed into the CSSOM (CSS Object Model), which is a render-blocking resource that must be completely constructed before proceeding to the next step.
3. JavaScript Execution
JavaScript is a parser-blocking resource. When the browser encounters a script tag, it pauses DOM construction to download and execute JavaScript. JavaScript can modify both DOM and CSSOM, so it must wait for CSS parsing to complete. Using async or defer attributes can change the execution timing.
4. DOM + CSSOM โ Render Tree
The DOM and CSSOM are combined to form the render tree, which includes only visible elements (elements with display: none are excluded) and their style information.
5. Layout๏ผReflow๏ผ
Calculates the exact position and size of each visible element, determining its positioning in the viewport. Changes in viewport size trigger a reflow.
6. Paint๏ผRepaint๏ผ
Converts the layout into actual pixels, including text, colors, borders, shadows, and other visual effects, typically performed across multiple layers.
7. Composite
Combines different painted layers into the final screen, handling stacking order and transparency. Using GPU acceleration can improve performance.
Performance Optimization Recommendations
Minimize Critical Resources
- โขReduce HTML, CSS, JavaScript file sizes
- โขRemove unused CSS/JavaScript
- โขUse Code Splitting and lazy loading
Reduce Reflow and Repaint
- โขBatch DOM modifications
- โขUse CSS Transform and Opacity for animations
- โขAvoid frequent changes to element positions and sizes
๐ฅ Common Interview Questions
(1) What are Reflow and Repaint? What is the difference between them?
Answer:
- โขReflow: Triggered when an element's size, position, or position in the document flow changes
- โขRepaint: Triggered when an element's appearance changes without affecting layout, such as changes in color or background
- โขReflow always causes Repaint, but Repaint doesn't necessarily cause Reflow
(2) Why is CSS considered a render-blocking resource?
Answer:
- โขCSS is considered a render-blocking resource because the browser must wait for the CSSOM to be fully constructed before building the render tree
- โขEven if JavaScript doesn't depend on CSS, JavaScript execution will be blocked if there are style sheets that haven't finished downloading
- โขThis is to ensure the correctness and consistency of page rendering
(3) What CSS properties affect Reflow and Repaint? How to avoid triggering them frequently?
Answer:
- โขProperties triggering Reflow: width, height, margin, padding, border, position, top, left, right, bottom, font-size, font-family, display, float
- โขProperties primarily triggering Repaint: color, background, visibility, text-decoration, box-shadow, outline
- โขHigh-performance properties: transform, opacity, filter (these properties typically only trigger compositing layers, processed with GPU acceleration, without triggering CPU-intensive Reflow operations)
Strategies to avoid frequent triggering:
// ------------------------- 1. Batch DOM modifications instead of individual changes -------------------------
// Bad practice - Multiple Reflow triggers
const element = document.getElementById('myElement');
element.style.width = '100px';
element.style.height = '200px';
element.style.margin = '10px';
// Good practice - Only one Reflow trigger
const element = document.getElementById('myElement');
element.classList.add('new-layout');
// Or use style.cssText
element.style.cssText = 'width: 100px; height: 200px; margin: 10px;';
/* CSS */
.new-layout {
width: 100px;
height: 200px;
margin: 10px;
}
// ------------------------- 2. Use DocumentFragment to batch add multiple elements -------------------------
const fragment = document.createDocumentFragment();
for (let i = 0; i < 10; i++) {
const li = document.createElement('li');
li.textContent = `Item ${i}`;
fragment.appendChild(li);
}
document.getElementById('myList').appendChild(fragment);
// ------------------------- 3. Use transform instead of position adjustments -------------------------
// Bad practice - Triggers Reflow
element.style.left = '10px';
element.style.top = '20px';
// Good practice - Only triggers compositing
element.style.transform = 'translate(10px, 20px)';
// ------------------------- 4. First set display: none, make changes, then show again -------------------------
element.style.display = 'none';
// Make multiple changes...
element.style.width = '100px';
element.style.height = '200px';
element.style.margin = '10px';
// Finally show it, triggering Reflow only once
element.style.display = 'block';