Js-javascript Advanced Programming Learning Note 21 ways to improve JavaScript performance

Source: Internet
Author: User

Tagged with: note lock C + + image declaration Extra Handler Insert Save

Chapter 24th Best Practices

1. performance

1. Avoid global lookups

A global object that will be used more than once in a function is saved in a local variable . For example, multiple use of document.getelement ... , you can save the document object in the local doc variable by first var Doc=document.

2. avoid the with Statement.

With creates its own scope and requires additional scope chain lookups

3 . Avoid unnecessary property lookups

If you need to use object properties multiple times , you should store them in a local variable .

4. Optimized Circulation

Reduced iterations are faster and optimize the loop body

5. Expand Cycle

The elimination cycle is faster when the number of cycles is Determined. The basic concept of the Duff device is to expand the loop.

6. Avoid double interpretation

JS parsing string code needs to start the parser, slow

7. Other

① Native method is faster . Because it is written in a compiled language such as C + +

The ②switch statement is Faster. Faster than If-else statements .

③ bit operators are faster .

8. Minimizing statements

① multiple variable declaration combinations

② Insert Iteration values, such as Var name=values[i++];

③ using arrays and object literals is better than using constructors

9. Optimize DOM Interaction

① minimizes field updates, such as inserting elements multiple times with the dom, you can create a document fragment, then insert the fragment and make only one field update .

② uses innerHTMLto create DOM structures faster than js. Should build good content to call innerHTML once, Avoid multiple innerhtml.

③ uses event delegation to reduce event handlers on the page with event Bubbling.

④ Note the htmlcollection object. Avoid multiple calls, It is dynamic, each call needs to be a document query!

The Htmlcollection object is returned in the following cases

    • Made a call to the getElementsByTagName
    • Gets the childNodes property of the element
    • Gets the attributes property of the element
    • Special Collections , such as document.forms/document.images, are Accessed.

Js-javascript Advanced Programming Learning Note 21 ways to improve JavaScript performance

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.