Data structure best practice regarding unique id's
<p>I have these two data structures that I continually find myself choosing between when pulling data from a database:</p>

<pre><code>{
    "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"}
    ]
}
</code></pre>

<p>Each seems to have pros and cons...</p>

<p>The object structure is great for quickly accessing a value given an id by saying <code>obj['3'].color</code> the problem is when listing all objects you have to loop with a <code>for(key in obj)</code> loop which seems to be very slow.</p>

<p>The array structure loops much faster using <code>for(var i=0; i&lt;array.length; i++)</code> but accessing a value given an id is not easy out of the box.  You have to make a function that loops through the entire array checking the id against a supplied parameter.</p>

<p>Here's a <a href="http://jsperf.com/for-in-vs-for-in-array" rel="nofollow">jsperf</a> of the two solutions.</p>

<p>Which do you think is better and why?</p>
