

What is Reflow and Repaint in Simple terms.md
source link: https://gist.github.com/gopal1996/de0b218253fcf8c9634bd8029d14eaf4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

What is Reflow and Repaint in Simple terms.md · GitHub
Recently I came to know about Reflow and Repaint. How it's affecting web performance. I am writing this post to give insights about reflow and repaint. Before Jumping into the topic, let's understand how the browser renders the website.
- When the user enters the URL, It will fetch the HTML source code from the server
- Browser Parse the HTML source code and convert into the Tokens (<, TagName, Attribute, AttributeValue, >)
- The Tokens will convert into the nodes and will construct the DOM Tree
- The CSSOM Tree will generate from the CSS rules
- The DOM and CSSOM tree will combine into the RenderTree
- The RenderTree are constructed as below:
- Start from the root of the dom tree and compute which elements are visible and their computed styles
- RenderTree will ignore the not visible elements like (meta, script, link) and display:none
- It will match the visible node to the appropriate CSSOM rules and apply them
- Reflow: Calculate the position and size of each visible node
- Repaint: now, the browser will paint the renderTree on the screen
Repaint and Reflow
- The Repaint occurs when changes are made to the appearance of the elements that changes the visibility, but doesn't affect the layout
- Eg: Visibility, background color, outline
- Reflow means re-calculating the positions and geometries of elements in the document. The Reflow happens when changes are made to the elements, that affect the layout of the partial or whole page. The Reflow of the element will cause the subsequent reflow of all the child and ancestor elements in the DOM
Both Reflow and Repaints are an expensive operation
According to Opera, most reflows essentially cause the page to be re-rendered:
Reflows are very expensive in terms of performance, and is one of the main causes of slow DOM scripts, especially on devices with low > processing power, such as phones. In many cases, they are equivalent to laying out the entire page again.
Here is a snapshot of how the browser draws user interface on the screen.
What Causes the Reflows and Repaints
- Adding, Removing, Updating the DOM nodes
- Hiding DOM Element with display: none (both reflow and repaint will occur)
- Hiding DOM Element with visibility: hidden (only repaint will occur, because no layout or position change)
- Moving, animating a DOM node
- Resizing the window
- Changing the font style
- Adding or removing Stylesheet
- Script manipulating the DOM
var bstyle = document.body.style; // cache
bstyle.padding = "20px"; // reflow, repaint
bstyle.border = "10px solid red"; // another reflow and a repaint
bstyle.color = "blue"; // repaint only, no dimensions changed
bstyle.backgroundColor = "#fad"; // repaint
bstyle.fontSize = "2em"; // reflow, repaint
// new DOM element - reflow, repaint
document.body.appendChild(document.createTextNode('dude!'));
Minimizing repaints and reflows
The strategy to reduce the negative effects of reflows/repaints on the user experience is to simply have fewer reflows and repaints and fewer requests for style information, so the browser can optimize reflows. How to go about that?
- Don't change individual styles, one by one. Best for sanity and maintainability is to change the class names, not the styles. If the styles are dynamic, edit the cssText property
// bad
var left = 10,
top = 10;
el.style.left = left + "px";
el.style.top = top + "px";
// better
el.className += " theclassname";
// or when top and left are calculated dynamically...
// better
el.style.cssText += "; left: " + left + "px; top: " + top + "px;";
-
Batch DOM Changes
- Use a documentFragment to hold temp changes
- Clone, update, replace the node
- Hide the element with display: none (1 reflow, 1 repaint), add 100 changes, restore the display (total 2 reflow, 2 repaint)
-
Don't ask for computed styles repeatedly, cache them into the variable
- Multiple reads/writes (like for the height property of an element)
- Writes, then reads, from the DOM, multiple times causing document reflows.
- Read(cached), write(invalidate layout), read(trigger layout).
- To fix: read everything first then write everything.
- Multiple reads/writes (like for the height property of an element)
-
Resources:
Chrome Tool Performance
Chrome provides a great tool that helps us to figure out what is going on with our code, how many reflows (layout) and repaint do we have, and more details about the memory, events, etc.
Bad code
var box1Height = document.getElementById('box1').clientHeight;
document.getElementById('box1').style.height = box1Height + 10 + 'px';
var box2Height = document.getElementById('box2').clientHeight;
document.getElementById('box2').style.height = box2Height + 10 + 'px';
var box3Height = document.getElementById('box3').clientHeight;
document.getElementById('box3').style.height = box3Height + 10 + 'px';
var box4Height = document.getElementById('box4').clientHeight;
document.getElementById('box4').style.height = box4Height + 10 + 'px';
var box5Height = document.getElementById('box5').clientHeight;
document.getElementById('box5').style.height = box5Height + 10 + 'px';
var box6Height = document.getElementById('box6').clientHeight;
document.getElementById('box6').style.height = box6Height + 10 + 'px';
Optimized Code
document.getElementById('box1').style.height = box1Height + 10 + 'px';
document.getElementById('box2').style.height = box2Height + 10 + 'px';
document.getElementById('box3').style.height = box3Height + 10 + 'px';
document.getElementById('box4').style.height = box4Height + 10 + 'px';
document.getElementById('box5').style.height = box5Height + 10 + 'px';
document.getElementById('box6').style.height = box6Height + 10 + 'px';
var box1Height = document.getElementById('box1').clientHeight;
var box2Height = document.getElementById('box2').clientHeight;
var box3Height = document.getElementById('box3').clientHeight;
var box4Height = document.getElementById('box4').clientHeight;
var box5Height = document.getElementById('box5').clientHeight;
var box6Height = document.getElementById('box6').clientHeight;
Resource:
Recommend
-
60
写在前面 在讨论回流与重绘之前,我们要知道: 浏览器使用流式布局模型 (Flow Based Layout)。 浏览器会把HTML解析成DOM,把CSS解析成CSSOM,DOM和CSSOM合并就产生了Render Tree。 有了RenderTree,我们就
-
70
-
49
很多人都知道要减少浏览器的重排和重绘,但对其中的具体原理以及如何具体操作并不是很了解,当突然提起这个话题的时候,还是会一脸懵逼。希望大家可以耐着性子阅读本文,仔细琢磨,彻底掌握这个知识点! 博客、前端积累文档、公众号、GitHub 网页生成过程: HTM
-
4
Closed Bug 1492538 Opened 2 years ago Closed 6 months ago...
-
15
What forces layout / reflow All of the below properties or methods, when requested/called in JavaScript, will trigger the browser to synchronously calculate the style and layout*. This is also called reflow or
-
16
Grow, shrink and reflow elements with Figma Auto LayoutPublished: 2020.12.22 | 2 minutes readLet’s build a quick prototype, literally just a few buttons. One rectangle, a bit of text and the button is ready. Easy-peasy!...
-
22
GUILayout: Mismatched LayoutGroup.repaint 回頭看到舊文
-
7
回流(reflow)与重绘(repaint)时间: 2021-07-26阅读: 27标签: 渲染分享扫一扫分享...
-
16
Versatile Reflow Oven Controller Uses ESP32-S2 [Maker.Moekoe] wanted a single controller board that was usable with different reflow ovens or hotplates. The result is a
-
3
Here's How Often You Should Repaint Your House, According To A Painter
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK