


Performance optimization of JavaScript (repaint and reflow)_Basic knowledge
repaint (repaint), when repaint changes, the element The appearance is changed, and it occurs without changing the layout. For example, changing the outline, visibility, and background color will not affect the rendering of the DOM structure.
reflow (rendering), the difference from repaint is that it will affect the rendering of the dom structure, and at the same time it will trigger repaint, it will change itself and all parent elements (ancestors), this overhead is very expensive, resulting in performance The decline is inevitable, and the more page elements there are, the more obvious the effect will be.
When it happens:
. Addition, modification (content), deletion (Reflow Repaint) of DOM elements
. Only modify the font color of DOM elements (only Repaint, because there is no need to adjust the layout)
. Apply new styles or modify any properties that affect the appearance of the element
. Resize browser windows, scroll pages
. Read certain properties of the element (offsetLeft, offsetTop, offsetHeight, offsetWidth, scrollTop/Left/Width/ Height, clientTop/Left/Width/Height, getComputedStyle(), currentStyle(in IE))
How to avoid:
. Delete the element from the document first, and then put the element back to its original position after completing the modification.
. Set the display of the element to "none", and then modify the display after completing the modification. is the original value
. If you need to create multiple DOM nodes, you can use DocumentFragment to create them and add them to the document at once
var fragment = document.createDocumentFragment();
fragment.appendChild(document.createTextNode('keenboy test 111'));
fragment.appendChild(document.createElement('br'));
fragment.appendChild(document.createTextNode('keenboy test 222'));
document.body.appendChild(fragment);
. Focus on modifying the style
4.1 Modify the attributes on the element style as little as possible
4.2 Try to modify the style by modifying the className
4.3 Set the style value through the cssText attribute
element.style.width=”80px”; //reflow
element.style.height=”90px” ; //reflow
element.style.border=”solid 1px red”; //reflow
The above will generate multiple reflows. The more calls are made, the more
element.style.cssText=” width:80px;height:80px;border:solid 1px red;”; //reflow
4.4 Caching Layout attribute value
var left=elem.offsetLeft; Using left multiple times will generate a reflow
4.5 Set the position of the element to absolute or fixed
The element is separated from the standard flow and also separated from the DOM tree structure. When reflow is needed, only reflow itself and subordinate elements
4.6 Try not to use table layout
table element Once reflow is triggered, it will cause all other elements in the table to reflow. When tables are suitable for use, you can set table-layout to auto or fixed, so that the table can be rendered line by line. This approach is also to limit the scope of influence of reflow
4.7 Avoid using expression, which will be displayed every time it is called. Recalculate again (including loading page)
Reference:
Yahoo! Performance Engineer Nicole Sullivan's latest article "Reflows & Repaints: CSS Performance making your JavaScript slow?"

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...
