JS Performance optimization

Source: Internet
Author: User

Here are some tips for optimizing the client JS performance:

1.[top] In the loop of JS, the loop is a common process control. JS provides three types of loops: for (;;), while (), for (in). In these three loops, for is the least efficient because it needs to query the hash key, so use the for (in) loop as sparingly as possible, and the for (;;) and while () loop performs basically flat. Of course, a for loop is recommended, and if the loop variable is incremented or decremented, do not assign a value to the loop variable alone, but instead use a nested + + or--operator.

2. If you need to iterate through an array, you should cache the length of the array, put the array length into local variables, and avoid querying the array length multiple times.

3. Local variables are accessed faster than global variables because the global variable is actually a member of the Window object, and the local variable is placed in the stack of the function.

4. Use of Eval as little as possible, it takes a lot of time to use the eval each time, the function template can be implemented by using the closure supported by JS.

5. Try to avoid nested queries of the object, for OBJ1.OBJ2.OBJ3.OBJ4 this statement, you need to do at least 3 query operations, first check whether obj1 contains Obj2, and then check obj2 contains Obj3, and then check obj3 contains obj4 ... This is not a good strategy. You should try to use local variables to save Obj4 as local variables to avoid nesting queries.

6. When making an operator, try to use +=,-=, *=, \=, and so on, instead of assigning operations directly.

7.[Top] When you need to convert a number to a character, use the following method: "" + 1. From a performance point of view, when converting a number to a character, you have the following formula: ("" +) > string () >. toString () > New String (). String () is an intrinsic function, so it is fast. and. ToString () to query the function in the prototype, so the speed is inferior, new string () needs to recreate a string object, the slowest.

8.[Top] When you need to convert a floating-point number to an integer, you should use Math.floor () or Math.Round (). Instead of using parseint (), this method is used to convert a string to a number. And math is an internal object, so Math.floor () does not actually have much query method and call time, and the speed is the fastest.

9. Try to create the object in JSON format instead of the Var Obj=new object () method. Because the former is a direct copy, and the latter needs to call the constructor, the former performs better.

10. When using arrays, try to use the JSON-formatted syntax, which defines the array directly using the following syntax: [Parrm,param,param ...] instead of 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 call the constructor of the array.

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

Finally there is a basic principle, for the large JS object, because the time and space to create the cost is relatively large, so should try to consider the use of caching.

This article turns from: Inter-water

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.