Writing several character-saving statements for the for loop in js

Source: Internet
Author: User
Tags array length


In javascript, the for loop is very commonly used. The syntax is as follows:


For (var I = 0; I <arr. length; I ++ ){
Var a = arr [I];
//...
    }

This is a common for loop with positive Order. The disadvantage of this writing is that it is a waste of performance to compare length and I from arr every time (and, if the length of arr is dynamic, ). The method to improve this loop is to save arr. length with the variable:


For (var I = 0, al = arr. length; I <al; I ++ ){
Var a = arr [I];
//...
    }
In this way, the performance can be slightly improved compared with the first one. If the number of leaders is the same, the performance can be improved more.

However, this write adds a variable al, which is only useful when compared with I.

If the cyclic order is not important to you, you can try the inverted loop:


For (var I = arr. length-1; I>-1; I --){
Var a = arr [I];
//...
    }
In this way, the number of variables is smaller, and the arr length is cached, so the performance is good. But the code here is a little poorly written (I intentionally), first I = arr. length-1 (actually-1, depending on), and then the condition I>-1 for loop execution, all make people with cleanliness unbearable.

Below are some of my commonly used reverse-order for loop statements:

For (var I = arr. length; I --;){
Var a = arr [I];
//...
    }
This is already very streamlined. The principle must be understood as follows: the condition for the for loop to continue to be executed is: yes; this judgment between; and here I-, when the first loop comes in, I = arr. length, I-value unchanged (why? Because I changes in the for loop body); when I = 1, I-or 1, but after entering the loop body, it is 0, so the last loop can be normally executed. When I = 0, I-is still 0, and 0 is not true, so the loop will not continue to be executed.

We noticed that in the for loop body of all the code above, there is a var a = arr [I], which is used to retrieve the array items from the current loop. This is actually a waste, and jsLint will tell you: do not declare variables in the loop...

Reverse-order for can be simplified to SS, but I just want a forward order with high efficiency and few variables. What should I do? As follows:


For (var I = 0, a; a = arr [I ++];) {
//...
    }
The advantage of this writing is that the arr. length is almost inevitable, and the previous sentence about retrieving the array items from the current loop is also missing.

Principle:

A = arr [I ++], which serves as a condition for loop execution. Note that there is only one = sign, so this is not a sentence, but a value assignment statement, it is to assign arr [I ++] to a and then judge whether a is a true value. I'm not talking about the principle types of I ++ and I-, but when I ++ has exceeded the length of the array, the loop must be stopped, and here it is actually stopped, why? Because a = arr [I ++], if an item beyond the length of the array is obtained, only one undefined is obtained, and undefined is a false value, and the loop condition determines that it fails.

Of course, the disadvantages of such writing are also obvious:
1. When the length of arr changes dynamically, an endless loop will still occur-because we have never cached arr. length.
2. If the loop is a numeric array, when the retrieved item (that is, the value of a) is 0, the loop will be aborted (because 0 is a false value ).
3. When one of the values in the array is a dummy value (including null strings, 0, null, and undefined), the loop is also aborted.

Therefore, when using this method, it is best to exclude the above situation and use it again.

This principle can also be used in reverse loops.

Finally, I would like to advise you a few words:

Code streamlining is not equal to high efficiency!
Do not deliberately compress the code to lose performance
Below are some key points to improve the for loop performance:

1. Timely break! A bounce condition is required if you do not need to traverse all objects!
2. Do not declare variables in the for loop body (one var and multiple values are recommended)
2. Cache the array length with as few variables as possible

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.