Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7753063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:57:32+00:00 2026-06-01T11:57:32+00:00

I have these two data structures that I continually find myself choosing between when

  • 0

I have these two data structures that I continually find myself choosing between when pulling data from a 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"}
    ]
}

Each seems to have pros and cons…

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

The array structure loops much faster using for(var i=0; i<array.length; i++) 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.

Here’s a jsperf of the two solutions.

Which do you think is better and why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:57:33+00:00Added an answer on June 1, 2026 at 11:57 am

    As always, the answer is: It depends. Will you mostly access the objects randomly by their id? Then use the object. Will you mostly loop through them in order? Then use an array.

    Of course, you can do both, by returning an array and then creating an index on it by id value. E.g.,

    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 periodically.)

    Now you can loop through them with a loop, or randomly access them via arr.index["some_id"]. Note that you have to be careful when modifying it (e.g., ensure you do deletions and additions in both places).

    Note that there I’ve used a property on the actual array, called index (the name can be whatever you like; I frequently use index or byId or similar). Some people don’t like to use non-index properties on arrays; I have no problem with it, since arrays are really just objects anyway. But you don’t have to, you can keep track of the index in its own variable as a peer of arr.

    Also note that while there may be an absolute difference in the speed of iteration using for..in on the object vs. for on the array, the odds of there being any real-world impact on that speed of iteration is very low.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these two data structures: public TreeMap<String, Integer> tableFrequency; public SortedSet<Map.Entry<String, Integer>> sortedTable;
I have these two questions that I think I understand how to answer (answers
I have these two methods on a class that differ only in one method
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I have a number of Data Transfer Objects (DTO's) that map onto data structures
I have these two structures in my domain: Exercise (with subjects, solution, difficulty ext.)
I have two sources: an xml-file containing definitions of several hundred data structures, basically
Two closely-related data structures are the suffix tree and suffix array. From what I've
I have an object which will be stored in two different data structures. I
I have two SQL tables with data that I would like to compare. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.