Optimizing the execution efficiency of JavaScript some methods summarize _javascript skills

Source: Internet
Author: User
Tags switch case
1, using an array join method in a lower version of the browser (such as IE6,IE7, etc.) is much more efficient than using the + number to connect (such as [' AAA ', ' BBB ', ' CCC '].join () more efficiently than ' AAA ' + ' BBB ' + ' CCC ');

2, Array:
Pop is more efficient than shift, and push is more efficient than unshift. This is important for designing the binary heap structure, and it is best to place the largest or smallest element at the end of the array.

3, digital rounding is best to use shift operation:
1.1 >> 0;

4. Use direct amount to create array and object:
var a = [];
var o = {};

5, the object level do not nest too much, reduce the search for objects:
Do not use A.B.C.D.E, this design way to get the E object.

6, the key value corresponding to the value, and switch case comparison, the key value corresponding to the switch case efficiency, each browser has been tested, you can see about this comparison of the article JavaScript Small experiment;

7. If you use JQ, there is also a $ (' xxxx '). Empty (). Append (' xxxxxxx '); and $ (' xxxxx '). HTML (' xxxxx '); The result is $ (' xxxx '). Empty (). Append ( ' xxxxxxx '); win, the article address is a small jquery experiment;

8. Cycle
In JavaScript, we can use the for (;;), while (), for (in) three loops, which are extremely inefficient in the three loops, because he needs to query the hash key, as long as it can be used sparingly. for (;;) The performance of the while loop should be the same as the basic (usually used) equivalent.

If the loop variable is incremented or decremented, do not assign a value to the loop variable alone, and you should use the nested + + or-operator when it last reads.

If you want to compare the length of an array, you should put the length property of the array into a local variable in advance, reducing the number of queries.

9, local variables and global variables
Local variables are faster than global variables, because global variables are actually members of global objects, and local variables are placed in the stack of functions.

10. Do not use eval
Using Eval is equivalent to invoking the interpretation engine to run the content at run time, which takes a lot of time. This time using JavaScript-supported closures enables functional templates (refer to functional programming for the contents of closures);

11. String connection
If you are appending a string, it is best to use the S+=ANOTHERSTR operation instead of using S=S+ANOTHERSTR;

12, the number into a string, the application of "" + 1, although it looks more ugly, but in fact this efficiency is the highest, performance up to say:

("+" > String () >. toString () > New String ()

This is actually a bit like the "direct volume" below, where you can use an internal operation that you use at compile time faster than the user operation used at run time.

A string () is an intrinsic function, so it is very fast, and. ToString () to query the function in the prototype, so the speed is somewhat inferior, the new String () is used to return an exact copy;

13, floating point conversion to integral type, this is more error-prone, many people like to use parseint (), in fact, parseint () is used to convert strings to numbers, rather than floating-point number and integer conversion between, we should use Math.floor () or Math.Round ().

14. String Traversal operation
Loop the string, such as substitution, lookup, you should use regular expressions, because the JavaScript itself is slower to cycle, and regular expression of the operation is written in C language API, performance is very good;

15, Timer
If you are working with code that is running, you should not use settimeout instead of setinterval. SetTimeout each time to reset a timer;

In the update ..., please wait!
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.