Summary of methods to optimize javascript execution efficiency

Source: Internet
Author: User
Tags switch case

1. In earlier browsers (such as IE6 and IE7), the join method of arrays is much more efficient than the join method of Number + (such as ['aaa ', 'bbb ', 'ccc']. join () is more efficient than 'aaa' + 'bbb '+ 'ccc );

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

3. It is best to shift the number to an integer:
1.1> 0;

4. directly create an Array and an Object:
Var a = [];
Var o = {};

5. Do not nest too many object layers to reduce Object Search:
Do not use a. B. c. d. e. This design method gets e objects.

6. The key value corresponds to the value. Compared with the switch case, the key value corresponds to the value, which is more efficient than switch case. All browsers have tested it. You can refer to the comparison Article javascript small experiment;

Comparison of values ('xxxxx'); the result is $ ('xxxxxx'). empty (). append ('xxxxxxxx'); the winner is the jQuery small experiment;

8. Loop
In JavaScript, we can use three types of loops: for (;), while (), and for (in). in these three types of loops, the efficiency of for (in) is very poor, because he needs to query hash keys, as long as they can be used as little as possible. The performance of for (;) and while loops is equivalent to that of normally used loops.

If it is a loop variable that increments or decreases, do not assign values to the loop variable separately. You should use nested ++ or-operators during the last read.

If you want to compare the length of the array, you should first put the length attribute of the array into a local variable to reduce 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 function stack.

10. Do not use Eval
Using eval is equivalent to calling the interpretation engine again at runtime to run the content, which consumes a lot of time. At this time, the closure supported by JavaScript can be used to implement the function template (for details about the closure, refer to the relevant content of functional programming );

11. String connection
If it is an append string, it is best to use the s + = anotherStr operation instead of using s = s + anotherStr;

12. convert a number into a string and apply "" + 1. Although it looks ugly, the efficiency is actually the highest. In terms of performance:

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

This is actually a bit similar to the following "Direct Volume". The internal operations that can be used during compilation are faster than those used during running.

String () is an internal function, so the speed is very fast, and. toString () is used to query functions in the prototype, so the speed is inferior. new String () is used to return an exact copy;

13. Converting a floating point to an integer is more prone to errors. Many people prefer parseInt (). In fact, parseInt () is used to convert a string to a number, rather than converting between a floating point and an integer, we should use Math. floor () or Math. round ().

14. String Traversal
Regular expressions should be used for loop operations on strings, such as replacement and search, because JavaScript itself is slow in the cycle, while regular expression operations are performed using APIs written in C language, good performance;

15. Timer
If you are targeting constantly running code, you should not use setTimeout, but setInterval. SetTimeout you need to reset a timer each time;

Updating ......, Coming soon!

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.