JavaScript Code Performance Optimization Summary (recommended) _javascript tips

Source: Internet
Author: User

Here are some tips I've summed up, just for reference.

The following code is basically in the jquery source can be seen, if the wrong place, please point out.

Use the source method as much as possible

JavaScript is an explanatory language and is slower to execute than a compiled language. The way the browser has been implemented, don't go through it again. In addition, the browser has implemented a number of methods in the algorithm has done a lot of optimization.

Avoid global lookups

In a function, a global object is stored as a local variable to reduce the global lookup because accessing the local variable is faster than accessing the global variable.

Try to reduce the number of cycles

Less than one cycle, you can improve the performance of several times. If you want to do multiple operations on each element of an array, use a loop as many times as possible, instead of looping multiple times, one operation at a time. In particular, when multiple regular matches are made, the regular expressions are merged as much as possible, and the corresponding matches are found as much as possible in one traversal.

Cycle

Switch

Conditional Branch

Arrange conditional branches, in probability order from high to Low: you can reduce the number of times the interpreter has probed the condition.

When >2 conditional branching in the same condition, the efficiency of using switch over If:switch branch selection is higher than if, especially in IE. 4 branch of the test, ie under the switch execution time is about half of if.

Replaces the conditional branch with the three-mesh operator.

Timer

If you are running code, you should not use settimeout instead of setinterval, because settimeout Initializes a timer every time, and setinterval only Initializes a timer at the beginning.

Another way to create objects-don't use new

Use Boolean types whenever possible for a variable used as a marker

Mark with true and false directly, not with numbers or 1 and 0来 of strings.

The above JavaScript code Performance optimization Summary (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.