BRUCE_FEBRUCE_FE

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.

ๅฝฑ็‰‡็ธฎๅœ–

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
                     โ”‚
                     โ†“
                Composite

1. 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';