Several writing methods and efficiency Summary of for loop in JavaScript

Source: Internet
Author: User
Every JS developer is inevitably dealing with the for loop. After all, this is one of the essential tools for traversing. However, when the number of cycles is large, the efficiency issue must be emphasized. The following article mainly introduces the writing and efficiency of several for loops in JavaScript. for more information, see the following. 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

Write declaration variables: for (var I = 0, len = arr. length; I <len; I ++ ){}

In addition to the for loop, there are also forEach (). Some articles also say that forEach () is the most efficient, and forEach () is recommended. Which of the following statements is the most 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

The code for measuring and executing the time. I use console. time () and console. timeEnd () for testing.

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 release'); var newArrs = []; for (var I = 0; I
 
  

Variable Declaration

Function testUseDeclare (testArrs) {console. time ('use release'); var newArrs = []; for (var I = 0, len = testArrs. length; I
   
    

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:

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. Because len = arr. length This arr. length may have been cached, it makes no sense 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 release'); var newArrs = []; for (var I = 0; I
     
      

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.

For more information about how to write and summarize the efficiency of the for Loop in JavaScript, see PHP!

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.