Effectively improve the efficiency of Javascript DOM operations

Source: Internet
Author: User

Bkjia.com document DOM performance bottleneckThe bottleneck of DOM operations is screen re-painting. Reducing the number of redraws may increase the efficiency of DOM operations.

Reduce re-painting to improve efficiency concentrate DOM operations at once as much as possible

Example: the efficiency of using temporary objects is poor.

Reference content is as follows:
For (var I = 0; I <items. length; I ++) {var item = document. createElement ("li"); item. appendChild (document. createTextNode ("Option" + I); list. appendChild (item);} better performance

Reference content is as follows:
Var fragment = document. createDocumentFragment (); for (var I = 0; I <items. length; I ++) {var item = document. createElement ("li"); item. appendChild (document. createTextNode ("Option" + I); fragment. appendChild (item);} list. appendChild (fragment );

Setting the DOM object to display: none is less efficient.

Reference content is as follows:
For (var I = 0; I <items. length; I ++) {var item = document. createElement ("li"); item. appendChild (document. createTextNode ("Option" + I); list. appendChild (item);} better performance list. style. display = "none"; for (var I = 0; I <items. length; I ++) {var item = document. createElement ("li"); item. appendChild (document. createTextNode ("Option" + I); list. appendChild (item);} list. style. display = ""; Use classname to replace the element with poor performance for multiple settings of the style. style. backgroundColor = "blue"; element. style. color = "red"; element. style. fontSize = "12em"; better performance. newStyle {background-color: blue; color: red; font-size: 12em;} element. className = "newStyle"; the cache DOM object has poor performance. document. getElementById ("myDiv "). style. left = document. getElementById ("myDiv "). offsetLeft + document. getElementById ("myDiv "). offsetWidth + "px"; var myDiv = document. getElementById ("myDiv"); myDiv. style. left = myDiv. offsetLeft + myDiv. offsetWidth + "px ";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.