Loop Optimization in Javascript

Source: Internet
Author: User
Loop is the basic function of most programming languages, and JS is no exception. The difference is that JS is an interpreted language and runs in a browser environment, the client's hardware and software conditions will have a great impact on the JS execution efficiency. However, the client environment is unknown and diverse for developers, and it is difficult to modify the... SyntaxHighlighter. all ();

Loop is the basic function of most programming languages, and JS is no exception. The difference is that JS is an interpreted language and runs in a browser environment, the client's hardware and software conditions will have a great impact on the JS execution efficiency. However, the client environment is unknown, diverse, and difficult to change for developers. Therefore, optimizing code quality is the main way to improve code efficiency.
In JS Code, loop is a factor that easily leads to performance problems. Understanding the characteristics of the loop and further targeted optimization may bring about a good performance improvement.
For, while, do-while loop:
The efficiency of these three cycles is similar, so you only need to select the appropriate application scenario.
Take the for loop as an example:
Var aValues = ["a", "B", "c", "d"];
For (var I = 0; I <aValues. length; I + = 1 ){
FDoSomethingA (aValues [I]);
FDoSomethingA (aValues [I]);
}
In the preceding example, the length of the array and I must be compared for each loop. Therefore, the length of the array must be re-read each time. If the length of the array remains unchanged in the loop, this is unnecessary, we can use local variables instead of length reading. Similarly, in this example, because aValues [I] is read more than twice, we can also assign it to a local variable:
Var aValues = ["a", "B", "c", "d"], nLength = aValues. length;
For (var I = 0, sValue; I <nLength; I ++ = 1 ){
SValue = aValues [I];
FDoSomethingA (sValue );
FDoSomethingB (sValue );
}
If the business logic of the loop is not sensitive to the loop sequence, you can try an inverted loop to reduce the counter to 0.
Var aValues = ["a", "B", "c", "d"], nLength = aValues. length;
For (var I = nLength, sValue; I-= 1 ;){
SValue = aValues [I];
FDoSomethingA (sValue );
FDoSomethingB (sValue );
}
In this way, the counter is compared with 0 by default, and even the comparison of local variables is omitted, which can also improve the efficiency theoretically.
For-in loop:
The for-in loop is more like a poor loop. It is used to traverse object attributes. We know that the search for object attributes will continue until the top of the prototype chain, which greatly reduces the cycle efficiency. There is no room for optimization in the writing of the for-in loop, so we need to follow the principle: we try to use the for-in loop only when traversing data objects.
If the properties of the traversal object are clear, use array loop instead.
For example, traverse a contact object:
Var aContact = ["N", "FN", "EMAIL; PREF",...];
For (var I = aContact. length; I-= 1 ;){
FDoSomething (aContact [I]);
}
Duff Policy
The primary principle of Duff policy is to increase the efficiency by expanding the number of cycles to reduce the number of times. For example
A common loop:
For (var I = aValues. length; I-= 1 ){
FDoSomething (aValues [I]);
}
If aValues. length = N, writing the following method is more efficient than following the cycle:
FDoSomething (aValues [0]);
FDoSomething (aValues [1]);
FDoSomething (aValues [2]);
FDoSomething (aValues [3]);
...
...
FDoSomething (aValues [N-1]);
However, if N is large, this method is unrealistic, and the Duff strategy is a moderate loop expansion strategy.
Recently, the Duff policy has been added to the initialization cycle of Netease mail address book contacts:
Var nLength = aContacts. length,
// The total number of rounds
NRounds = Math. floor (nLength/8 ),
// Additional margin
NLeft = nLength % 8,
I = 0;
// Handle the margin first
If (nLeft ){
Do {
FFormat (aContacts [I ++]);
} While (-- nLeft)
}
// Perform 8 formatting every round
If (nRounds ){
Do {
FFormat (aContacts [I ++]);
FFormat (aContacts [I ++]);
FFormat (aContacts [I ++]);
FFormat (aContacts [I ++]);
FFormat (aContacts [I ++]);
FFormat (aContacts [I ++]);
FFormat (aContacts [I ++]);
FFormat (aContacts [I ++]);
} While (-- nRounds)
}
As shown above, 8 contact data can be formatted in each cycle, and another cycle is used to process the remaining contacts. It can be seen that when there are many contacts, the total number of cycles is greatly reduced, which can reduce the consumption of cycles. In addition, 8 is the optimal value proposed by the Duff policy.
In actual tests, IE can improve performance by more than 10-20%, rather than in IE browsers.
Conclusion: In the test process, we found that the efficiency difference between the optimized and the pre-optimized browsers is not very large, or even can be ignored. This shows that the JS engine of these browsers

 


From Netease mail front-end technology center

Related Article

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.