This article has participated in the third “topic writing” track of the Denver Creators Training Camp. For details, check out: Digg Project | Creators Training Camp third is ongoing, “write” to make a personal impact.
Note: Part of the article content and pictures from the network, if there is infringement please contact me (homepage public number: small siege lion learning front)
By: Little front-end siege lion, home page: little front-end siege lion home page, source: Nuggets
GitHub: P-J27, CSDN: PJ wants to be a front-end siege lion
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.
The browser parses the rendered page
The process is briefly
After the browser kernel gets the content, the rendering steps can be roughly divided into the following steps:
-
Parse the HTML and build the DOM tree
-
Parses the CSS and generates the CSS rule tree
-
Combine DOM tree and CSS rules to generate render tree
-
Render tree (Layout/reflow), responsible for each element size, location calculation
-
Render the render tree to draw the page pixel information
-
The browser sends each layer’s information to the GPU, which then composes the layers and displays them on the screen
OS:– Reflow: also known as layout, the Chinese is called backflow/rearrangement, generally means that the content, structure, position or size of the element has changed, the need to recalculate the style and render tree, this process is called reflow.
Repaint: When the changes to the element only affect the appearance of the element (e.g. background color, border color, text color, etc.), just apply the new style to the element.
The process,
1. Parse the DOM tree based on HTML
- According to the content of HTML, the tag is parsed into a DOM tree according to the structure, and the process of DOM tree parsing is one
Depth-first traversal
. That is, all children of the current node are built before the next sibling node is built. - In the process of reading the HTML document and building the DOM tree,
If a Script tag is encountered, construction of the DOM tree is paused until the script completes execution.
2. Generate a CSS rule tree based on CSS resolution
Js execution is paused while the CSS rule tree is parsed until the CSS rule tree is ready
.Browsers do not render until the CSS rule tree is generated
.
3. Generate a rendering tree by combining DOM tree and CSS rule tree
- After the DOM tree and CSS rule tree are all ready, the browser will start
Building a Render tree
. - Simplifying CSS can also speed up the building of CSS rule trees, resulting in faster pages.
4. Calculate the information of each node according to the render tree (layout)
- Layout: Calculates the layout of each render object from the render object information in the render tree
Position and dimensions
- Backflow: After the layout is complete, it is found that something has changed that affects the layout and needs to be rewound
Re-render.
5. Draw the page based on the calculated information
- In the rendering phase, the system will traverse the rendering tree and call the renderer’s
"Paint" method
To display the contents of the renderer on the screen. - Redraw: Attributes such as the background color, text color, etc. of an element that do not affect the layout around or inside the element will only cause the browser to redraw.
- Backflow: If the size of an element changes, the render tree needs to be recalculated and re-rendered.
Consider: How to reduce backflow and redraw?
We all know that Reflow takes more time than Repaint and therefore has a greater impact on performance. Try to avoid too many reflows. So what’s the solution?
Let’s start by reviewing the flow of the rendering pipeline:
To minimize backflow and redraw times and improve performance, we first need to know the trigger conditions.
Common situations that trigger backflow:
- The geometry of a DOM element changes. Common geometry properties are
width
,height
,padding
,margin
,left
,top
,border
Wait, that’s easy to understand. - Make the DOM node happen
Increase or decrease
ormobile
. - Read and write
offset
The family,scroll
andclient
In order to retrieve these values, the browser needs to perform a backflow operation. - call
window.getComputedStyle
Methods.
Now let’s look at the backflow process.
Following the rendering pipeline above, when backflow is triggered, if the DOM structure changes, the DOM tree is re-rendered and the rest of the process (including tasks outside the main thread) is completed.
It’s like going through the parsing and composition process all over again, and it’s very expensive.
Common situations that trigger redraw:
Repaint occurs when DOM changes result in style changes that do not affect geometry properties.
Redraw process: Since there are no changes to the DOM geometry, the element position information does not need to be updated, eliminating the need for layout. The process is as follows:
Skip the stages of generating layout trees and building layer trees, directly generate the drawing list, and then continue the following series of operations, such as partitioning, bitmap generation.
So redrawing doesn’t necessarily cause backflow, but backflow must have redrawn.
Knowing the principles above, what are the implications for the development process?
- Avoid using style too often, and instead modify it
class
The way. - use
createDocumentFragment
Perform batch DOM operations. - For resize, scroll, etc., perform anti-shake/throttling.
- Add will-change: Tranform and have the rendering engine implement a separate layer for it. When these transformations occur, only the composition thread is used to process them without involving the main thread, greatly improving rendering efficiency. Of course, this change is not limited to
tranform
, any CSS property that can be synthesizedwill-change
To declare.
PS: To learn more about redrawing and repainting, check out my article 🔥 What is Redrawing and rearranging? How to avoid it?
Thank you for reading, I hope to help you, if there is a mistake or infringement of the article, you can leave a message in the comment area or add a public number in my home page to contact me.
Writing is not easy, if you feel good, you can “like” + “comment” thanks for your support ❤