Read JavaScript High performance programming key notes _javascript Tips

Source: Internet
Author: User

1th

Efficient concise                     //Low Energy
children        //childnodes
childelementcount            //childnodes.length
FIRSTELEMENTC Hild              //firstchild
lasteelmentchild              //lastchild
nextelementsibling
//nextsibling Previouselementsibling       //previoussibling

2nd: The efficient application of selectors

<div id= "Footer_bottom" >
<a href= "/aboutus.aspx" > Cloud-dwelling Community </a><a href= "/contactus.aspx" > Contact us </a><a href= "/about/ad.aspx" > Advertising service </a><a href= "/about/job.aspx" > Talent Service </a>© 2006-2016 <a href= "http://www.jb51.net/" > Cloud-dwelling community </a>
</div>
var aarr1= document.queryselectorall ("#footer_bottom a");/concise efficient
var aArr2 = Docuement.getelementbyid ("Footer_ Bottom "). getElementsByTagName (" a ")//complex inefficient

//return 
<a href="/aboutus.aspx "> Cloud-
dwelling community </a> <a href= "/contactus.aspx" > Contact us </a> <a
href= "/about/ad.aspx" > Advertising service </a>,
<a href = "/about/job.aspx" > Talent service </a>

//Select the first child node
var a = Document.queryselector ("#footer_bottom a");
return <a href= "/aboutus.aspx" > Cloud Habitat </a>

//Select multiple nodes
var divs = Document.queryselectorall (" Div.footer,div.main,div.header ");

Note: Most browsers support the above properties, ie6,7,8 only supports children properties

Reduced DOM rendering and typesetting (three ways)

1. The elements to be processed are hidden, processed, and finally displayed

2. How to create a file fragment (recommended) document.createdocumentfragment ();

3. Clone a copy of the element to be processed, then manipulate the copy, and finally replace the copy with the original

The following is a supplement to the small series of cloud-dwelling communities

Storing the objects that are looping

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++) {}

The cycle of the performance of the consumption is very large, the cycle of the object storage, reduce each cycle to do the object calculation.

Minimize reflow and redraw

Before use:

var coored = document.getElementById ("Ctghoteltab");
document.getElementById ("Ctghoteltab"). Style.top = Coored.offsettop + + "px";

After use:

var coored = document.getElementById ("Ctghoteltab"),
offettop = Coored.offsettop +;
document.getElementById ("Ctghoteltab"). Style.top = offettop + "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.