Reflow and Repaint
How the browser renders the document?
- Receives the data (bytes) from the server.
- Parses and converts into tokens (<, TagName, Attribute, AttributeValue, >).
- Turns tokens into nodes.
- Turns nodes into the DOM tree.
- Builds CSSOM tree from the
css rules
. - CSSOM and DOM trees are combined into a Render Tree.
- Computes which elements are visible and their computed styles.
- Starting from the root of the dom tree.
- Not visible elements like (
meta
,script
,link
) anddisplay: none
are omitted from the render tree. - For each visible node, find the appropriate matching CSSOM rules and apply them.
Repaint
Occurs when changes affect the visibility. For example, opacity
, color
(the alpha channel), background-color
, visibility
.
Reflow (Layout)
- Occurs when the changes affect the layout.
- Triggers:
width
,position
,float
, change DOM, scrolling. - Recalculate of positions and sizes.
- Has a bigger impact, changing a single element can affect all children, ancestors, and siblings or the whole document.
Minimizing Repaints and Reflows
- Batch DOM changes
- Don't ask for computed styles repeatedly, cache them into variable.
- Use ID-Based Selectors.