Home > Article > Web Front-end > Redraw and reflow in the rendering phase: which one is more impactful?
Redraw and reflow: Which impact does it have on the rendering phase?
In front-end development, performance optimization has always been an important issue. Among them, reducing repaint and reflow operations is the key to improving page performance. However, many developers are not clear about the concept and impact of redraw and reflow. This article will detail the concepts of redraw and reflow and their impact on the rendering phase, illustrated with concrete code examples.
First, let’s understand the concepts of redraw and reflow. Redrawing is an operation that changes the appearance of an element without affecting its layout properties. For example, change the background color, font color, etc. Reflow refers to changing the layout attributes of elements, such as changing the size, position, etc. of elements. When we make modifications to the DOM, the browser will perform redraw and reflow operations to update the page.
Frequent redrawing and reflow will lead to performance degradation. First of all, redrawing and reflow will trigger the re-execution of the rendering pipeline, which will consume a certain amount of computing resources. Secondly, redrawing and reflow will cause the page to be redrawn, which may cause page flickering or freezing, giving users a bad experience. Therefore, reducing redraw and reflow operations is critical to improving page performance and user experience.
Next, we use specific code examples to illustrate the impact of redrawing and reflow on the rendering phase. Suppose we have a list, each list item in it has a style with class item, and we want to change the background color of each list item:
<ul id="list"> <li class="item">列表项1</li> <li class="item">列表项2</li> <li class="item">列表项3</li> <li class="item">列表项4</li> </ul>
First, we use JavaScript to change the background color of the list item Background color:
var items = document.getElementsByClassName('item'); for (var i = 0; i < items.length; i++) { items[i].style.backgroundColor = 'red'; }
The above code will cause the browser to perform a redraw operation. When modifying the background color of each list item, it will only change the appearance attributes of the element and will not affect the layout attributes of the element.
Next, let’s modify the code and change the operation of changing the background color to changing the size of the element:
var items = document.getElementsByClassName('item'); for (var i = 0; i < items.length; i++) { items[i].style.width = '100px'; items[i].style.height = '100px'; }
This code will trigger the reflow operation because we have changed the size of the element. Affects the element's layout properties.
As can be seen from the above code examples, redraw and reflow operations have different effects on the rendering phase. Redrawing only changes the appearance properties of the element, while reflow changes the layout properties of the element. In actual development, we should try to avoid frequent redrawing and reflow operations. We can reduce performance loss through the following optimization methods:
1. Use CSS properties such as transform and animation to achieve animation effects. They do not trigger reflow operations and perform better.
2. Reduce the number of redraws and reflows by modifying the DOM in batches.
3. Use DocumentFragment to reduce the number of DOM operations and merge multiple DOM operations into one.
To sum up, redrawing and reflowing are important factors that affect page performance. Developers should understand their concepts and impacts, properly optimize code, and reduce the occurrence of redraw and reflow operations to improve page performance and user experience.
Reference materials:
The above is the detailed content of Redraw and reflow in the rendering phase: which one is more impactful?. For more information, please follow other related articles on the PHP Chinese website!