Read Javascript high-performance programming key notes, javascript high-performance

Source: Internet
Author: User

Read Javascript high-performance programming key notes, javascript high-performance

First point

// Efficient and concise // Low Energy Dissipation children // childNodeschildElementCount // childNodes. lengthfirstElementChild // firstChildlastEelmentChild // lastChildnextElementSibling // response // previousSibling

Second: efficient application of Selector

<Div id = "footer_bottom"> <a href = "/AboutUS. aspx "> helping customers </a> <a href ="/ContactUs. aspx "> Contact Us </a> <a href ="/about/ad. aspx "> advertisement service </a> <a href ="/about/job. aspx "> Talent Service </a>©2006-2016 <a href = "http://www.bkjia.com/"> help house </a> </div>
Var certificate R1 = document. querySelectorAll ("# footer_bottom a"); // simple and efficient var partition r2 = docuement. getElementById ("footer_bottom "). getElementsByTagName ("a"); // complicated and inefficient // return <a href = "/AboutUS. aspx "> helper house </a>, <a href ="/ContactUs. aspx "> Contact Us </a>, <a href ="/about/ad. aspx "> advertisement service </a>, <a href ="/about/job. aspx "> Talent Service </a>, // select the first subnode var a = document. querySelector ("# footer_bottom a"); // return <a href = "/AboutUS. aspx "> helper house </a> // select multiple nodes var divs = document. querySelectorAll ("div. footer, div. main, div. header ");

Note: Most browsers support the preceding attributes. IE6, 7, and 8 only support the children attribute.

Reduce DOM re-rendering and formatting (three methods)

1. Hide the elements to be processed, process them, and display

2. How to Create File fragments (recommended) document. createDocumentFragment ();

3. clone a copy of the element to be processed, operate on the copy, and replace the original

The following is a supplement to the help house editor

Store cyclic objects

Before use:

var str = "nanananana";for (var n = 0; n < str.length; n++) {}

After use:

 var str = "nanananana",strLgth = str.length;for (var n = 0; n < strLgth ; n++) {}

Loop consumes a lot of performance. It stores cyclic objects to reduce the calculation of objects in each loop.

Minimize backflow and re-painting

Before use:

var coored = document.getElementById("ctgHotelTab");document.getElementById("ctgHotelTab").style.top = coored.offsetTop + 35 + "px";

After use:

var coored = document.getElementById("ctgHotelTab"),offetTop = coored.offsetTop + 35;document.getElementById("ctgHotelTab").style.top = offetTop + "px";

Related Article

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.