Client JS performance Optimization tips Finishing _javascript Skills

Source: Internet
Author: User
Tags array length
Here are some tips on optimizing client JS performance:

1. About the JS cycle, the cycle is a common flow control. JS provides three kinds of loops: for (;;), while (), for (in). In these three loops, for (in) is the least efficient because it needs to query the hash key, so as little as possible in the for (in) loop, for (;;), while () loop performance is basically flat. Of course, it is recommended to use a For loop, and if the loop variable is incremented or decremented, do not assign a value to the loop variable alone, but you should use the nested + + or--operator.

2. If you need to traverse the array, you should first cache the array length var len=arr.length; for (i=0;i<len;i++), put the length of the array in a local variable to avoid querying the length of the array multiple times.

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

4. Use eval as little as possible, consuming a lot of time each time you use Eval, especially within a loop, json[i][variable]=1; Such statements do not use Eval.

5. Try to avoid the object of nested query, for OBJ1.OBJ2.OBJ3.OBJ4 this statement, you need to do at least 3 query operations, first check whether the obj1 contains Obj2, and then check the OBJ2 contains Obj3, and then check the OBJ3 contains obj4 ... This is not a good strategy. Local variables should be used as much as possible to save OBJ4 with local variables, thus avoiding nested queries.

6. Make operators, as far as possible using +=,-=, *=, \= and other operational symbols, rather than directly to the assignment operation.

7. When you need to convert numbers to characters, use the following: "" + 1. In terms of performance, when you convert numbers to characters, you have the following formula: ("+" > String () >. toString () > New String (). String () is an intrinsic function, so it is very fast. and. ToString () to query the function in the prototype, so less speed, new string () needs to re-create a string object, the slowest.

8. When you need to convert floating-point numbers to integers, you should use either Math.floor () or Math.Round (). Instead of using parseint (), the method is used to convert a string to a number. And math is an internal object, so Math.floor () actually doesn't have a lot of query methods and call times, and the speed is the fastest.

9. Maximize the JSON format to create objects instead of the Var Obj=new object () method. Because the former is a direct copy, and the latter needs to invoke the constructor, the former is better performance.

10. When you need to use arrays, try to use the JSON-formatted syntax, which is to define the array directly using the following syntax: [Parrm,param,param ...] instead of using the new array (Parrm,param,param ...) This syntax. Because the syntax used in JSON format is directly explained by the engine. The latter needs to invoke the constructor of the array.

11. A regular expression is used to iterate over a string, such as substitution, lookup. Because JS cycle speed is relatively slow, and regular expression of the operation is written in C API, performance is better.

Finally, there is a basic principle, for large JS objects, because the creation time and space overhead are relatively large, so should be considered as far as possible to use caching.

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.