Data structure best practice for unique identifiers

I have these two data structures that I constantly choose between when you fetch data from the database:

{
    "1": {"location": "seattle", "color": "red"},
    "2": {"location": "irvine", "color": "blue"},
    "3": {"location": "san diego", "color": "green"}
}

{
    "listings":[
        {"id": "1", "location": "seattle", "color": "red"},
        {"id": "2", "location": "irvine", "color": "blue"},
        {"id": "3", "location": "san diego", "color": "green"}
    ]
}

      

It seems like they all have pros and cons ...

The object structure is great for quickly accessing the value given by the identifier, pointing out the obj['3'].color

problem is listing all the objects you should be using with a loop for(key in obj)

that seems very slow.

The array structure is much faster using for(var i=0; i<array.length; i++)

, but accessing the value given by the identifier is not easy out of the box. You have to make a function that will loop through the whole array, checking the id against the supplied parameter.

Here's the jsperf of two solutions.

What do you think is better and why?

+3


source to share


1 answer


As always, the answer is: It depends . Will you mostly access objects randomly by theirs id

? Then use the object. Will you mostly focus on them in order? Then use an array.

Of course, you can do both by returning an array and then creating an index on it with a value id

. For example.

var arr = /*...wherever you get your second example, the array...*/;
var index, len, entry;
arr.index = {};
for (index = 0, len = arr.length; index < len; ++index) {
    entry = arr[index];
    arr.index[entry.id] = entry;
}

      

(I have a function that does this because I find it a useful technique from time to time.)



Now you can loop through them or access them arbitrarily through arr.index["some_id"]

. Note that you have to be careful when modifying (for example, make sure you add and remove in both places).

Note that there I used a property on the actual array called index

(the name can be whatever you like, I often use index

or byId

or similar). Some people dislike using non-indexed properties on arrays; I have no problem with this, since arrays are indeed objects anyway . But you don't need that, you can keep track of the index in your variable as equal arr

.

Also note that while there could be an absolute difference in iteration speed using for..in

vs. for

in an array, the probability that any real world impact will occur at this iteration rate is very low.

+1


source







All Articles