How should I write the for loop of JS arrays?

Source: Internet
Author: User

Then let's see which authority I began to doubt today...

Since I started programming, and since I came into contact with the array, I have been seeing reminders at different places and at different times: when using for to traverse arrays, you must use for (VAR I = 0, n = arr2.length; I <n; I ++, instead of using for (VAR I = 0; I> arr. length; I ++), because you can use your mind to think about it. The second part of the second method has been to calculate the length of the array, so the efficiency is relatively low.

oh? We will not talk about other Program languages here, but only about Js. Different languages may have different implementations, we need to explore other languages. In fact, the so-called "Think with your mind" mentioned above may only be because you only think with your mind, rather than thinking carefully or trying it yourself. So now let's take a closer look. Will the first write method be faster than the second one? Does arr. Length consume a lot of CPU? No. Why is CPU consumption? Arr. length does not call a method, but only reads the Length attribute of the array. Which of the following statements reads the native attribute and the defined variable quickly? I think reading length will be faster, so I wrote a test to test my idea: I used my own small test framework, copy Code the code is as follows: var arr = [], arr2 = [], I = 0
while (I <100000) {
arr. push (I)
arr2.push (I)
I ++
}< br> M. ta. begin ("0000");
for (VAR I = 0; I arr [I] = arr [I] * arr [I] * arr [I]
}< br> M. ta. end ("0000", "For (VAR I = 0; I M. ta. begin ("0001");
for (VAR I = 0, n = arr2.length; I arr2 [I] = arr2 [I] * arr2 [I] * arr2 [I]
}< br> M. ta. end ("0001", "For (VAR I = 0, n = arr2.length; I M. ta. showresult ()

Of course, this code is abnormal and occupies over 300 MB of memory. The result is as follows:
Chrome

Firefox

(After N performance tests, I found that Firefox is much more efficient in basic operations than chrome, but it is much slower when rendering is involved)
IE8

(After N orders of magnitude are reduced, the above script cannot be run in IE)
Conclusion :?
In fact, this test is not intended to emphasize for (VAR I = 0; I <arr. length; I ++) is much faster, because testing also has some errors, just to show that this writing method will not be slow, and this writing method has some flexibility, the writing is also simple and the amount of code is small. Why don't we use it? If it is the first method, what happens when the length of the array changes during the loop? This cannot be solved.

In fact, I still respect the authority, so I am still worried when writing it here. Why am I wrong? If so, let's just smile. If not, I finally wrote a blog post about the human model dog...
Reprinted note:
Http://www.html-js.com

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.