Home >Web Front-end >JS Tutorial >Collections and efficiency in JavaScript_javascript skills

Collections and efficiency in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:37:02923browse

Although the key of this collection can only be of String type, unlike various collection classes in Java that can use various objects as Key, it is enough for realizing general client JS functions. Similarly, because all JS internal objects inherit from the Object object, the Array object of JS can actually use strings as array subscripts, just like array variables in PHP. From Bird Food Xuan.

Array is an internal object provided by JavaScript. It is a standard collection. We can add (push) and delete (shift) the elements in it. We can also traverse the elements in it through a for loop. In addition to the array Can we have other collections in JavaScript?

Due to the language features of JavaScript, we can dynamically add and remove properties to universal objects. So Object can also be regarded as a special collection of JS. Let’s compare the characteristics of Array and Object:

Array:

New: var ary = new Array(); or var ary = [];
Add: ary.push(value );
Delete: delete ary[n];
Traverse: for ( var i=0 ; i < ary.length ; i ) ary[i];

Object:

New: var obj = new Object(); or var obj = {};
Add: obj[key] = value; (key is string)
Delete: delete obj[key];
Traversal: for ( var key in obj ) obj[key];

From the above comparison, we can see that Object can be used as a collection. Use the Popup window to create an infinite web page menu (3) I introduced the __MenuCache__ implemented by Eric, which is also a simulated collection object.

If we want to retrieve a specified value in Array, we need to iterate through the entire array:

Copy code The code is as follows:

var keyword = ;
for ( var i=0 ; i < ary.length ; i )
{
if ( ary[i] = = keyword )
{
// todo
}
}

And to retrieve an entry with a specified key in Object, we only need to use:

var key = '';
var value = obj[key];
// todo

This feature of Object can be used to efficiently retrieve Unique string collections. The time complexity of traversing Array is O(n), while the time complexity of traversing Object is O(1). Although the cost of for retrieval for 10,000 collections is only tens of ms, if it is 1,000*1,000 retrievals or more, the advantages of using Object will be immediately apparent. Before this, I did a mapping to map 100 unique characters to a 1000 string array, which took 25-30s! Later, the for traversal was changed to the member reference of the collection simulated by Object. The same amount of data mapping took only 1.7-2s!!!

For the traversal efficiency of the collection (from high to low): var value = obj[key]; > for ( ; ; ) > for ( in ). The least efficient one is for(in). If the collection is too large, try not to use for(in) to traverse.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn