Critical rendering path
- Parse HTML -> DOM
- Request further images, scripts, stylesheets
- Scripts -> Render block (Pauses HTML) | Defer/async solves
- Browser optimizes for download
- Parse CSS -> CSSOM
- DOM + CSSOM = Render tree (Recalculate styles in dev tools)
- Render tree = DOM - Head - (Display none items) + (Pseduo elements :after :before)
- Each node as style close to it
- Layout
- For each node - width, height, position
- Change in width of parent affects all the descendants in it
- Paint - Pixel by pixel paint
- Caused by change in
backgroundImage
,box-shadow
orcolor
etc
- Composite - Individual layers together
Parse HTML
DOM is constructed incrementally, as the bytes arrive on the "wire"
When transfers happens the DOM starts buildings
- Bytes > Chars > Tokens > Nodes > DOM
- HTML5 - spec in how tree building
<link>
discovered, network request sent | Meantime DOM constructedDOM construction completes but blank screen Until CSS is downloaded
- Screen is empty, blocked on CSS
- Otherwise, flash of unstyled content (FOUC)
First CSS byte arrives
- Not incremental - cause of the cascading nature
- Wait for entire file
- Split style sheets - better (incremental reading)
CSSOM complete - but page blank
DOM + CSSOM = Render tree
Layout process

Paint process

Image decode & resize

Composite process

Dynamic changes
- Color change - repaint
- Position - Reflow (Re-layout) + repaint - nodes around it - margin change
- Position - Reflow (Re-layout) + repaint - nodes around it - margin change
- Major Changes - Reflow + repaint - font change
- Major Changes - Reflow + repaint - font change
Preload scanner phase
- Requests high priority resources (images, scripts, stylesheets, fonts)
- if this phase not present
- Pares HTML -> Request script -> execution (Sync manner)
- If phase present then (Async manner)
- Pre-scans done asyc loading of all files
Key performance metrics
- FCP - First contentful paint - First DOM elements painted
- FMP - First meaningful paint - Time for biggest element on the screen
- T2I - Time to interactive - Time until user inputs are responsive
Optimizing for critical rendering path
- Use defer/async scripts
- Reduce nested DOM elements
- Reduce file size by compression (gzip, br) or minification
- Lazy loading - Request files that are needed for initial load
- Hardware accelerated animations
- Add
transform: translate3D(0,0,0)
(intimidate for new layer) orwill-change: tranform
(request for new layer) - Compositing (layer on top of each other) would be handled by GPU
Reference
@ragavkumarv
swipe to next ➡️