Does javascript support associating arrays? Is access more efficient than objects?

Source: Internet
Author: User
For example, I had always mistakenly thought that the js cannot define correlated arrays like php, for example, a [& quot; color & quot;] red; but it was accidentally discovered a few days ago. Would you like to ask if this definition method is more efficient than the operation object? Are the two backend implementation methods different? Definition object obj... as mentioned in the question, I always mistakenly thought that the js cannot define the associated array as php, such as a ["color"] = red;
But it was accidentally discovered a few days ago.
Would you like to ask if this definition method is more efficient than the operation object? Are the two backend implementation methods different?

// Define the object
Obj = {color: "red", name: "apple "};
// Associate an array
Arr = [];
Arr ["color"] = "red"; arr ["name"] = "apple ";
Obj = []; obj. push (arr );

Reply content:

For example, I mistakenly thought that the js cannot define the correlated array like php, for example, a ["color"] = red;
But it was accidentally discovered a few days ago.
Would you like to ask if this definition method is more efficient than the operation object? Are the two backend implementation methods different?

// Define the object
Obj = {color: "red", name: "apple "};
// Associate an array
Arr = [];
Arr ["color"] = "red"; arr ["name"] = "apple ";
Obj = []; obj. push (arr );

The Javascript Data Group is also an object. At the same time, any object can also be used as a data group, as shown in the following figure.

See: http://jayconrod.com/posts/52/a-tour-of-v8-object-representation

Zookeeper solves the V8 object reality.

The actual situation of the object is divided into two parts:
  • Named properties: in-object properties and extra properties

  • Numbered properties: fast elements

The modes include array mode and dictionary mode. In general, the array mode is used, and sometimes the loss occurs.

Named properties

In-object properties uses a public map to describe its structure. Some conditions (using a shared map) are downgraded to the Dictionary mode.

V8 can handle minor pergences like this just fine, but if your code assigns all sorts of random properties to objects from the same constructor in no participating order, or if you delete properties, v8 will drop the object into dictionary mode, where properties are stored in a hash table. this prevents an absurd number of maps from being allocated.

A process called In-object slack tracking is used to determine the size of the object. The subsequent response is as follows: extra properties uses a single volume of data storage.

Now I'm sure your next question is, "what happens when a new property is added after-object slack tracking is complete? "This is handled by allocating an overflow array to store the extra properties. The overflow array can always be reallocated with a larger size as new properties are added.

Numbered properties

Among them, fast elements includes

Fast small integers
Fast doubles
Fast values

However, fast elements may also downgrade to the dictionary mode.

If you assign to an index that's way past the end of your the elements array, V8 may downgrade the elements to dictionary mode.

Of course, due to split-up memory, downgrading does not affect the stability of other types.

Because named properties and elements are stored separately, even if an object drops into dictionary mode for elements, named properties may still be accessed quickly (and vice versa ).

Using Array as an Associative Array does not increase performance.

Amount... In the shortest point, the following two are 'equivalence:

  1. Obj. attr = value
  2. Obj ['attr'] = value

Only 2 supports obj ['attr '+ var] = value, which is more flexible, such as a loop statement.

Just try the console.

arr=[];arr["color"]="red";arr["name"]="apple";console.log(arr);console.log(arr.length);

This is actually no different from using objects.

In javascript, an object is an associated array, and an associated array is an object. The object chapter of the javascript authoritative guide is described.

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.