Several methods and efficiency Summary of for loop in JavaScript, javascriptfor
Preface
For the for loop, I believe it is not common. But this is because I didn't understand the meaning of a for loop when I read the code.
This for loop is written as follows:
for (var i = 0, rule; rule = rules[i++];) { //do something}
What does this statement mean? Later, I am selling a customs gateway. I think it is quite good.
Impact of for loop writing on Efficiency
Before talking about the above Code, let's talk about the efficiency of the for loop. There are quite a lot of articles about the for loop writing method and the impact on efficiency when dealing with js. But in general, there are two ways to write for a for Loop:
- Do not write declaration variables:
for(var i = 0;i<arr.length;i++){}
- Writing declaration variables:
for(var i = 0,len = arr.length;i < len;i++){}
Besides the for loopforEach()
Also, some articles have said:forEach()
Most efficient, recommendedforEach()
Which of the following statements is highly efficient? Let's take a test.
Test Plan
The overall test scheme is as follows:
- Make a test array variable that can hold tens of millions.
- Use the for loop and foreach statements to traverse the test variable.
- Perform 10 tests on the same stable machine and obtain the average value.
- Test environment: CPU: Inter (R) Core i5-3210M, RAM: 12GM, system: win10 (x64)
Test procedure
Create Test variables
Use the while loop to make a test variable. This is very simple. The details are as follows:
var testArrs = [], i = 0;while(i<40000000){ testArrs.push(i); i++;}
Compile corresponding test functions
Code for measuring and executing the time, which isconsole.time()
Andconsole.timeEnd()
.
For these three for loops, we first make three functions. They are
Foreach loop test:
function testForeach(testArrs){ console.time('foreach'); var newArrs = []; testArrs.forEach(function(i){ newArrs.push(i); }); console.timeEnd('foreach');}
For Loop without declaring variables:
function testNoDeclare(testArrs){ console.time('no declare'); var newArrs = []; for(var i = 0;i<testArrs.length;i++){ newArrs.push(i); } console.timeEnd('no declare');}
Variable Declaration
function testUseDeclare(testArrs){ console.time('use declare'); var newArrs = []; for(var i = 0,len = testArrs.length;i<len;i++){ newArrs.push(i); } console.timeEnd('use declare');}
Execute the test function
It's easy to execute the test function. You just need to call the function.
testForeach(testArrs);testNoDeclare(testArrs);testUseDeclare(testArrs);
Test Results
After 10 tests, the following results are obtained:
Foreach |
Do not write a statement |
Write statement |
2372.891 ms |
672.530 ms |
743.974 ms |
2431.821 ms |
710.275 ms |
805.676 ms |
2422.448 ms |
729.287 ms |
741.014 ms |
2330.894 ms |
730.200 ms |
755.390 ms |
2423.186 ms |
703.255 ms |
769.674 ms |
2379.167 ms |
689.811 ms |
741.040 ms |
2372.944 ms |
712.103 ms |
710.524 ms |
2316.005 ms |
726.518 ms |
726.522 ms |
2535.289 ms |
733.826 ms |
747.427 ms |
2560.925 ms |
793.680 ms |
817.098 ms |
Average Value |
Average Value |
Average Value |
2414.56 ms |
720.15 ms |
755.83 ms |
I wonder if the results have surprised you? I did not expect that the most common writing method is the most efficient. Why? I don't want to understand either of them. If anyone knows, let me know, but I guess the statement writing is meaningless. Becauselen = arr.length
Thisarr.length
It may have been cached, so it is meaningless to declare a len variable to store it.
Complete the test code and copy it to your computer to test the Code. If there is anything unreasonable, please let me know.
var testArrs = [], i = 0;while(i<40000000){ testArrs.push(i); i++;}function testForeach(testArrs){ console.time('foreach'); var newArrs = []; testArrs.forEach(function(i){ newArrs.push(i); }); console.timeEnd('foreach');}function testNoDeclare(testArrs){ console.time('no declare'); var newArrs = []; for(var i = 0;i<testArrs.length;i++){ newArrs.push(i); } console.timeEnd('no declare');}function testUseDeclare(testArrs){ console.time('use declare'); var newArrs = []; for(var i = 0,len = testArrs.length;i<len;i++){ newArrs.push(i); } console.timeEnd('use declare');}testForeach(testArrs);testNoDeclare(testArrs);testUseDeclare(testArrs);
Special for loop writing
Next, let's talk about the code that I didn't understand at the beginning of the article, and explain the for loop syntax that I was not familiar with before. The basic syntax of the for Loop is:
For (Statement 1; Statement 2; Statement 3) {executed code block}
- Statement 1: run the statement before the loop (code block) starts.
- Statement 2: define conditions for running cycles (code blocks)
- Statement 3: executed after the loop (code block) has been executed
If we use the for loop to output 1 to 10, we can write it like this:
for(var i=0;i<10;i++){console.log(i);}
But! According to the preceding syntax, we can also write
for(var i=10;i--;){console.log(i);}
I was wondering at the beginning. How can I write this? Statement 2 places the loop condition and I-the judgment condition. Otherwise, in statement 2, if true is returned, the execution will continue. In js, when 0, null, undefined, false, ''," is used as a condition, the result is false, that is, when I-to 0 is false, the loop ends.
Return to the code at the beginning of the article
for (var i = 0, rule; rule = rules[i++];) { //do something}
This rule = rules [I ++] is the judgment condition. When it becomes undefined, the loop will be terminated. Therefore, this code is written as follows:
for(var i = 0;i < rules.length;i++){ var rule = rules[i]}
In fact, it is to put the judgment and value assignment together, and assign values while repeating. Is it quite simple?
Summary
The above is all the content of this article. I hope the content of this article will help you learn or use Javascript. If you have any questions, please leave a message.