Traversal of JavaScript arrays

Source: Internet
Author: User
Tags array arrays return variable

Array traversal and attributes
Although arrays are objects in JavaScript, there is no good reason to use a for in loop to traverse an array.
On the contrary, there are some good reasons not to use for-in traversal arrays.
Note: The array in JavaScript is not an associative array.
Only objects in JavaScript can manage the corresponding relationship of key values. But associative arrays are kept in order, and objects are not.
Because the for in loop enumerates all the properties on the prototype chain, the only way to filter these properties is by using the hasOwnProperty function,
So it will be many times slower than the normal for loop.
Traverse
In order to achieve the best performance of traversing arrays, the classic for loop is recommended.
var list = [1, 2, 3, 4, 5, ... 100000000];
for (var i = 0, L = list.length i < l; i++) {
Console.log (List[i]);
}
The code above has a process of caching the length of an array through L = list.length.
Although length is an attribute of an array, there is a performance overhead in accessing it in each loop.
Perhaps the latest JavaScript engine has been optimized for this, but we can't guarantee that our code is running on these latest engines.
In fact, it is much slower than cached versions to not use the length of the cached array.
Length Property
The getter method of the length property simply returns the length of the array, and the setter method truncates the array.
var foo = [1, 2, 3, 4, 5, 6];
Foo.length = 3;
Foo [1, 2, 3]
Foo.length = 6;
Foo [1, 2, 3]
Translator Note:
View in Firebug the value of Foo at this time is: [1, 2, 3, undefined, undefined, undefined]
But the results are not accurate, and if you look at the results of Foo on the Chrome console, you will find that: [1, 2, 3]
Because undefined is a variable in JavaScript, note that a variable is not a keyword, so the meaning of the above two results is completely different.
For verification, we'll execute the following code to see if ordinal 5 exists in Foo.
5 in Foo; return false either in Firebug or Chrome
FOO[5] = undefined;
5 in Foo; return true either in Firebug or Chrome
Setting a smaller value for length truncates the array, but increasing the length property value does not affect the pairs.
Conclusion
For better performance, it is recommended that you use a normal for loop and cache the length property of the array.
Traversing an array with a for in is considered a bad code habit and tends to generate errors and cause performance problems.



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.