JavaScript also supports associative arrays? Is access more efficient than the object?

Source: Internet
Author: User
Tags php definition
Title, the original has been mistakenly thought JS inside can not be like PHP definition associative array, such as a["color"]=red;
But a few days ago, I found it possible.
Would you like to ask if this is a more efficient way of defining than manipulating objects? Is the background of the two implementations inconsistent?

Defining objects
Obj={color: "Red", Name: "Apple"};
Associative arrays
Arr=[];
arr["Color"]= "red"; arr["name"]= "Apple";
Obj=[];obj.push (arr);

Reply content:

Title, the original has been mistakenly thought JS inside can not be like PHP definition associative array, such as a["color"]=red;
But a few days ago, I found it possible.
Would you like to ask if this is a more efficient way of defining than manipulating objects? Is the background of the two implementations inconsistent?

Defining objects
Obj={color: "Red", Name: "Apple"};
Associative arrays
Arr=[];
arr["Color"]= "red"; arr["name"]= "Apple";
Obj=[];obj.push (arr);

The number of Javascript is also the same, and at the same time any pair can also be used as a group, detailed below.

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

Detailed interpretation of the V8 of the image of the reality.

The image is divided into two layers:

    • Named properties:in-object properties and extra properties

    • Numbered Properties:fast elements

The pattern has array mode and dictionary mode. The general situation uses the array mode, which sometimes falls down.

Named Properties

In-object Properties uses a public map to describe its structure, and some situations (difficult to share map) will be downgraded to Dictionary mode.

V8 can handle minor divergences like this just fine, but if your code assigns all sorts of the random properties to objects fr Om the same constructor in no particular order, or if you delete properties, V8 'll drop the object into dictionary mode, The where properties is stored in a hash table. This prevents a absurd number of maps from being allocated.

A process called IN-OBJECT slack tracking is used to determine the size of the image, and the subsequent properties of the extra properties are stored using individual numbers.

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

Numbered properties

Among them, fast elements includes

Fast small integers
Fast Doubles
Fast values

However, fast elements can sometimes be downgraded to dictionary mode

If you assign to a index that's the past the end of your The elements array, V8 may downgrade the elements to dictionary Mode.

Of course, the level does not affect the other types of nature because it is stored separately.

Because named properties and elements is stored separately, even if an object drops to dictionary mode for elements, NA Med properties may still is accessed quickly (and vice versa).

CI, using an array as a associative array does not increase performance.

The amount ... At a shallow level, the following 2 are ' equivalent ':

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

Only 2 inside support obj[' attr ' +var]=value, can be more flexible, such as a circular statement inside.

Console try it.

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

There's no difference between doing this and using objects.

In JavaScript, an object is an associative array, and an associative array is an object. The JavaScript authoritative Guide Objects section has said.

  • 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.