Some ways to optimize javascript execution efficiency are summarized

  • 2020-03-30 01:04:29
  • OfStack

1, in the lower version of the browser (such as IE6,IE7, etc.) string concatenation using the array join method is much more efficient than using the + sign (such as ['aaa',' BBB ',' CCC ']. Join () than 'aaa'+' BBB '+' CCC 'efficient);

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

3, the number of the best use of the shift operation:
1.1 > > 0;

4. Create Array and Object with direct quantity:
Var a = [];
Var o = {};

5. The object hierarchy should not be nested too much to reduce the search of objects:
Do not use A.B.C.D.E. this is the design way to get the e object.

6. The corresponding value of key value is compared with that of switch case. The corresponding value of key value is more efficient than that of switch case.

7. If you use jq, there is also a $(' XXXX ').empty().append(' XXXXXXX '); And $(" XXXXX "). The HTML (" XXXXX "); $(' XXXX ').empty().append(' XXXXXXX '); Win, the article address is jQuery small experiment;

8, circulation
In JavaScript, we can use for(;;) While (),for(in), the three loops are extremely inefficient, because he needs to query the hash key, should use as little as possible. For (;;) The performance of the while loop should be basically equivalent to that of the while loop.

If the loop variable is increasing or decreasing, do not assign a value to the loop variable separately, use the nested ++ or - operators when it is last read.

If you want to compare the length of an array, you should first place the length attribute of the array in a local variable to reduce the number of queries.

9. Local variables and global variables
Local variables are faster to access than global variables because global variables are actually members of global objects and local variables are placed on the stack of functions.

Don't use Eval
Using eval is equivalent to calling the interpretation engine again at run time to run the content, and it takes a lot of time. In this case, you can use the closures supported by JavaScript to implement a functional template (see functional programming for more on closures).

11. String concatenation
If the string is appended, it is better to use s+=anotherStr instead of s=s+anotherStr;

12, the number into a string, the application of "" + 1, although it looks a bit ugly, but in fact this is the most efficient performance:

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

This is a bit like the "direct quantities" below, where it's faster to use internal operations that you can use at compile time than user operations that you can use at run time.

String() is an internal function, so it's fast, whereas toString() is not as fast as it would be if it were querying the function in the prototype, so new String() returns an exact copy;

Many people like to use parseInt(), but parseInt() is for converting strings to Numbers, not between floats and integers. We should use math.floor () or math.round ().

14. String traversal operation
For string loop operations, such as replacement, search, should use regular expressions, because the JavaScript itself is relatively slow loop speed, and the operation of regular expressions is written in C language API, performance is very good;

15. Timer
If you are dealing with running code, you should not use setTimeout, but setInterval. SetTimeout reset the timer one time;

Update... , stay tuned!

Related articles: