I’ve heard of both reflux and redraw. Whenever I talk about performance optimization, I’ll talk about this topic, so I’ll write it down to deepen my memory. I hope that when WE discuss it later, some details will come to mind and I won’t have to search on the Internet.
💠First nuggets of this article: simply record reflow and Repaint
Browser engine rendering process
To get started, familiarize yourself with the general process of browser DOM rendering, which consists of four main steps:
Parse the HTML structure and build a DOM tree
Build the render tree
Layout of the render tree
Draw the render tree
The detailed process is as follows:
-
Analyze THE HTML structure and build a DOM tree
-
Analyze CSS styles and parse out style rule sheets
-
Associate a DOM tree with a style rule table to build a Render tree (the process of building is called an Attachment)
-
Starting at the root of the DOM tree, each visible node is traversed
-
Head, meta, title and other nodes will not be added to the render tree and will not affect the render output
-
The style display: None is also ignored [1]
-
-
For each visible node, find and match the corresponding style rule
-
-
Start laying out the nodes in the Render tree (i.e. determine the coordinates and sizes of each node)
-
Once you have the coordinates, the browser traverses the Render tree to draw the individual nodes
Note: [1] Different from visibility: hidden, visibility: hidden will set the element to invisible and occupy a space on the page, while display: None removes the node from the entire Render tree and can be interpreted as not being part of the layout.
What is reflux and redraw
The two rendering behaviors that can occur during rendering the render tree we just saw are called backflow and redraw.
Reflow
When the geometry of a DOM node changes (e.g., the width and height of the DOM node change),
The browser will examine all other affected areas and recalculate their size and location
The DOM nodes are then automatically arranged and finally redrawn, a process known as reflow.
When the DOM is facelifted, it triggers reflow.
Repaint
If the image, color, and shadow of a DOM node change, that is, no layout or layout issues are involved, the DOM node can be directly drawn without layout. This process is called repaint.
DOM is simply made up to trigger repaint.
Triggering scenarios
Reflow
- When JS manipulates the DOM
- Set the style attribute/change the element visibility (
display: none
) - The element
width/height
,fontSize
,border
The modified animation
å’Œtransition
- Read some attributes of the element itself (
offsetWidth
.offsetHeight
.getComputedStyle
(), etc.) scroll
Scroll event- Window size changes
Repaint
- Color modification
- Text direction modification
- Shadow modification
conclusion
reflow
The trigger recalculates the size and position of the node, and may trigger itchildren
Nodes,parent
Node and other nodesreflow
, high costrepaint
It doesn’t have to triggerreflow
, butreflow
Bound to lead torepaint
.- Due to the
repaint
Does not affect the layout, so compared toreflow
The overhead is very low
In fact, the browser can choose to wait until the thread ends and then re-flow and modify the DOM. This means that if these changes happen fast enough in the same thread, they might only result in one reflow
That is, most modern browsers have optimized reflow to wait for a large enough number to do a batch before triggering reflow
Keeping the number of reflows to a minimum
Some optimization
- As far as possible to reduce
DOM
operation - To reduce
DOM
Nested hierarchy - Try not to use inline styles
- Avoid the use of
table
Lay out,table
Changes in the size and content of each element in thetable
Recalculation of. To switch todiv
Can effectively avoid unnecessaryreflow
å’Œrepaint
- For complex animations, set them up
position: fixed/absolute
Keep elements out of the document flow as much as possible to minimize impact on other elements
References:
Render-tree Construction, Layout, and Paint
Vue Core Virtual DOM(VDOM)
Rendering: repaint, reflow/relayout, restyle
Keeping the number of reflows to a minimum