JavaScript Code Performance Optimization Summary "Go"

Source: Internet
Author: User

See this article, turn around first. From: http://ued.ctrip.com/blog/javascript-code-performance-optimization-summary.html

The following code is basically in the jquery source code inside can see, if have said wrong place, please point out.

Use the source method as much as possible

JavaScript is an explanatory language that is slower to execute than a compiled language. The way the browser has been implemented, do not implement it again. In addition, the methods that the browser has implemented have done a lot of optimizations in terms of algorithms.

Avoid global lookups

Global objects are stored as local variables in a function to reduce global lookups, because accessing local variables is faster than accessing global variables.

Minimize cycle times

A few loops, you can improve the performance of several times. If you want to perform multiple operations on each element of an array, use a loop as much as possible, multiple operations instead of multiple loops, one operation at a time. In particular, when making multiple regular matches, merge the regular expressions as much as possible and find the corresponding matches in a single traversal.

Cycle

Switch

Conditional Branch

Arrange conditional branching from high to low in order of probability: You can reduce the number of times the interpreter detects a condition.

In the same condition >2 conditional branching, using switch better than If:switch branch selection is more efficient than if, especially in IE. 4 branches of the test, ie under switch execution time is about half the IF.

Use the three-mesh operator to override conditional branching.

Timer

If you are targeting code that is constantly running, you should not use settimeout, but should be using setinterval, because settimeout Initializes a timer every time, and setinterval Initializes a timer at the beginning.

Another way to create an object-not using new

Variables used as tokens use Boolean types whenever possible

Mark directly with true and false, and do not use numbers or 1 of the string to mark.

JavaScript Code Performance Optimization Summary "Go"

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.