From the data structure analysis, we can see that using for each... in is faster than for... in.

Source: Internet
Author: User

I have heard that Firefox's JS engine supports the for each in syntax, such as the following code:

Copy codeThe Code is as follows:
Var arr = [10, 20, 30, 40, 50];
For each (var k in arr)
Console. log (k );

You can directly traverse the content of the arr array.

Because only FireFox supports this feature, almost all JS Code does not use this feature.

However, the syntax for each is naturally supported in ActionScript. No matter Array, Vector, or Dictionary, as long as it is an enumerable object, both for in and for each in can be used.

I didn't feel much difference before. in order to be too lazy to knock an each word, I have been traversing it with a familiar for in.

However, I have carefully thought about it today. From the perspective of Data Structure, I think there is an essential difference between the efficiency of for in and for each in, whether it is JS or.

The reason is simple:Array is not an Array in the true sense!

What is an array of true meaning? Of course, it is the data type defined by type [] in traditional languages. All elements are continuously stored.

"Array" is also an Array, but anyone familiar with JS knows that it is actually a Non-Linear Pseudo Array, and the subscript can be any number. Writing data to arr [1000000] does not really apply for a space containing 1 million elements, but converts 1000000 to a corresponding hash value, corresponding to a small storage space, thus saving a lot of memory.

For example, the following array is available:

Copy codeThe Code is as follows:
Var arr = [];
Arr [10] = 1000;
Arr [20] = 2000;
Arr [30] = 5000;
Arr [40] = 8000;
Arr [200] = 9000;

Traversing Array with for... in is a very cumbersome process:

During each access to arr [k], a Hash (k) calculation is performed. Based on the Capacity modulo of the Hash list, the results are finally found in the conflicting linked list.

If the syntax for each... in is supported, the internal data structure will be much faster:

The Array stores the keys list and Associates each values value as a linked list. Each time a value is added or deleted, its link is updated.

For each... in, you only need to iterate from the first node, without any Hash computing.

Of course, for a linear array like Vector in AS3, there is little difference between the two. Similarly, the ArrayBuffer array in html5. However, theoretically, even if arr is a continuous linear array, for each in still needs to be faster:

For... for each access to arr [k], the subscript out-of-bounds check is performed. for each in directly generates iteration variables from the underlying layer based on the internal linked list, this saves time for cross-border checks.

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.