From the data structure analysis look: Use for each...in faster than for...in _ basic knowledge

Source: Internet
Author: User

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

Copy Code code as follows:

var arr = [10,20,30,40,50];
For each (var k in arr)
Console.log (k);

You can directly traverse the contents of the ARR array.

Because only Firefox support, so almost all of the JS code does not use this feature.

In ActionScript, however, the syntax for each is inherently supported, regardless of array or vector, or dictionary, as long as enumerable objects can be in and for every.

There was no big difference before, in order to bother knocking on a each word, it was always traversed in the familiar for.

But today, after careful thinking about it, from the point of view of the data structure, I think there is an essential difference in the efficiency of for and for, whether it is JS or as.

The reason for this is simple: arrayis not the real thing!

What is the true meaning of an array? Of course, the data type defined by type[in the traditional language, all elements are persisted continuously.

Although "array" is also the meaning of the array, but familiar with JS know, it is actually a non-linear pseudo array, subscript can be any number. Write arr[1000000] does not really request space to hold 1 million elements, but instead converts 1000000 to a corresponding hash value, which corresponds to a small piece of storage space, thus saving a lot of memory.

For example, there are the following arrays:

Copy Code code as follows:

var arr = [];
ARR[10] = 1000;
ARR[20] = 2000;
ARR[30] = 5000;
ARR[40] = 8000;
ARR[200] = 9000;

Traversing an array with for...in is a cumbersome process:

Every time we visit arr[k], we have to do a hash (k) calculation, according to the capacity of the hash table, finally found the result in the conflict list.

If you support the syntax for each...in, the internal data structure determines how much faster:

The array stores the list of keys, and also associates each values value as a chain list. Whenever a value is added or deleted, its link relationship is updated.

When a for each...in is traversed, it is only possible to iterate backwards from the first node without any hash calculation.

Of course, for a linear array such as the AS3 vector, the difference is small; Similarly, the HTML5 array arraybuffer supports binary arrays. In theory, though, even if ARR is a continuous, linear array, the for every in is a bit faster:

For...in traversal, each access arr[k] to check for the subscript out of bounds, and for every in according to the internal linked list, directly from the bottom of the feed back to the iteration variables, saving the process of cross-border inspection.

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.