Summary and Comparison of Several Writing Methods of for Loop Sentences in Javascript

  • 2021-07-13 04:20:10
  • OfStack

Preface

for loops are useful when js traverses objects or arrays. Today, let's look at some examples of for loops. The specific operation details are described below.

1 The general writing is as follows:


for(var i = 0;i< arr.length;i++) {
 var a = arr[i];
 //...
 }

This is a common for loop with positive order loop. The drawback of this writing is well known: comparing length with i from arr every time wastes performance (and if the length of arr changes dynamically, there will be an infinite loop). The way to improve this loop is to save it with variables arr.length :


for(var i = 0, al = arr.length;i< al;i++) {
 var a = arr[i];
 //...
 }

This can slightly improve the performance compared with the first one, and if the array is long, it can improve more.

However, writing in this way adds a variable al, and this variable is only useful when compared with i, which looks a little chicken ribs.

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


for(var i = arr.length-1;i > -1;i--) {
 var a = arr[i];
 //...
 }

In this way, there are fewer variables, and the length of arr is cached, so the performance is also good. But the code here is a bit poorly written (I deliberately). First of all, i = arr.length-1 (actually-1, by), and then the condition that the loop continues to execute i > -1. People with cleanliness can't stand it.

The following is my common reverse for loop writing:


for(var i = arr.length;i--;) {
 var a = arr[i];
 //...
 }

This has been very streamlined. Principle needs to understand 1: for loop continues to execute the conditions, is; ; This judgment between should be true, and i here, when the first loop comes in, i=arr.length When i=1, i is still 1, but after entering the loop, it is 0, so the last loop can be executed normally; when i=0, i is still 0, and 0 is no longer true, so the loop will not continue.

Notice that in the for loop body of all the above code, there is a var a = arr[i] Which is used to fetch the array item currently looping to. This is actually a waste, and jsLint and so on will tell you not to declare variables in the loop. . .

Reversed for can be reduced to Si, but I just want positive order, 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 it is almost inevitable arr.length It's gone, so is the previous sentence that takes out the array item that is currently looping to.

Principle:

a = arr[i++] In this case, as the condition that the loop can execute, notice that there is only one = sign here, so this is not a judgment sentence, it is an assignment statement, or it is a arr[i++] Assign it to a, and then judge whether a is true or not. I won't talk about the principle types of i + + and i. I just say that when i + + has exceeded the length of the array, the loop must stop, and here it really stops. Why? Because a=arr[i++] If you get an item that exceeds the length of the array itself, you will only get 1 undefined, and if undefined is a false value, the loop condition will fail.

Of course, the shortcomings of this writing are also obvious:

1. When the length of arr changes dynamically, there will still be an infinite loop-because we have never cached arr. length.

2. If the loop is an array of 1 numbers, the loop will be aborted when the fetched item (that is, the value of a) is 0 (because 0 is a false value).

3. When one item in the array is a false value (including empty string, 0, null, undefined), the loop will also be aborted

Therefore, when you use this writing method, it is best to exclude the above situation and reuse it.

This principle can also be applied to reverse loops.

Finally, I would like to give you a few words of advice:

Code simplification is not equal to high efficiency! Don't lose performance in order to deliberately streamline your code

Incidentally, I would like to say a few key points to improve the performance of for cycle:

1. Timely break! You don't need to traverse all of them, but you need to add jump-out conditions!

2. Do not declare variables in the body of for loop (it is recommended to assign values once var and multiple times)

2. Array length cache with as few variables as possible

Summarize

The above is the whole content of this article. I hope the content of this article can bring 1 certain help to your study or work. If you have any questions, you can leave a message for communication.


Related articles: