With the development of the network, speed and the increase of machine speeds, more and more sites have been used to enrich the client technology. And now Ajax is one of the most popular ways. JavaScript is an interpreted language, so it's impossible to reach levels like C/java, limit what it can do on the client, and in order to improve his performance, I'd like to talk about my experience based on a lot of the tests I've done for JavaScript. Hopefully, we can help you improve your JavaScript scripting performance.
Language level aspects
Cycle
Loop is a very common control structure, most of the things to rely on it to complete, in JavaScript, we can use for (;;), while (), for (in) three kinds of loops, in fact, these three kinds of loops in the efficiency of the very poor, because he needs to query the hash key, Use as little as you can. for (;;) And while loop performance should be said basic (usually used) equivalent.
In fact, how to use these two loops, there is a great emphasis. I have some very interesting situations in the test, see the Appendix. The final conclusion is:
If the loop variable is incremented or decremented, do not assign a value to the loop variable alone, it should use a nested + + or-operator when it is last read.
If you want to compare the length of the array, you should put the long property of the array in a local variable and reduce the number of queries.
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 in the stack of functions.
Do not use eval
Using Eval is equivalent to invoking the interpretation engine again at run time to run the content, which can take a lot of hours. The function template can be implemented by using the closures supported by JavaScript (refer to the relevant contents of the functional programming for the contents of the closures)
Reduce object Lookups
Because of the explanatory nature of JavaScript, A.B.C.D.E requires at least 4 queries, checking A and then checking the B in a, and checking the C in B, so down. So if the expression repeats itself, as long as it is possible, the expression should be as few as possible, you can use the local variable, put it in a temporary place to query.
This can be combined with the loop, because we often have to loop according to the length of the string, array, and usually this length is constant, such as every query a.length, an additional operation, and the pre-len=a.length Var, then one less query.
String connection
If you are appending strings, it is best to use the S+=ANOTHERSTR operation instead of using S=S+ANOTHERSTR.
If you want to concatenate multiple strings, you should use + = less, as
S+=a;s+=b;s+=c;
should be written
S+=a + B + C;
It is best to use a cache if you are collecting strings, such as multiple + = operations on the same string. How to use it? Use a JavaScript array to collect, and finally join using the Join method, as follows
The following is a reference fragment:
var buf = new Array (), for (var i = 0; i <; i++) {Buf.push (i.tostring ());} var all = Buf.join ("");
Type conversions
Type conversion is a common mistake, because JavaScript is a dynamic type language, and you cannot specify the type of the variable.
1. Convert the number to a string, apply "" + 1, although it looks uglier, but in fact this efficiency is the highest, performance said:
The following is a reference fragment:
("" +) > string () >. toString () > New String ()
This is a bit similar to the following "direct volume", as far as possible, the internal operations that can be used at compile time are faster than the user actions that are used at run time.
String () is an intrinsic function, so it is fast, and. ToString () queries the function in the prototype, so the speed is inferior, and new String () is used to return an exact copy.
2. Floating-point conversion to Integer, this is more error-prone, many people like to use parseint (), in fact, parseint () is used to convert a string into a number, rather than floating-point numbers and integers between the conversion, we should use Math.floor () or Math.Round ().
In addition, unlike the problem in the second section of object lookup, math is an internal object, so Math.floor () does not actually have much of a query method and call time, and the speed is the fastest.
3. For custom objects, if the ToString () method is defined for type conversion, it is recommended to explicitly call ToString (), since the internal operation attempts to convert the ToString () method of the object to a string after attempting all possibilities. So it is more efficient to call this method directly.
Turn JS tuning