Optimize javascript execution speed

Source: Internet
Author: User

1: Modify the execution method of the loop

For (var I = 0; I <element. childNodes. length; I ++ ){
Execute Code
}
In this way, the value of element. childNodes. length must be set
Var n = element. childNodes. length
For (var I = 0; I <n; I ++ ){
Execute Code
}

2: Number of dom modification operations
Dom inserts a batch of similar nodes in batches. Instead, it is better to construct an object and insert it once.
For example, the following method is reasonable:
Var div = document. createElement ("div ");
For (var I = 0; I <10; I ++ )[

Var element = document. createElement ("");
A. href = "";
Div. appendChild (element );

}
// Insert the object once after Construction
Document. body. appendChild (div );
3: reduces the reference layers of objects and reduces the complexity of browser parsing objects.

For example, a method named net. Loader. LoadXml (params)
You can
Var loadXml = net. Loader. LoadXml;
LoadXml (params );

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.