arrays format (Javascript)

Posted by João Melo on Programmers See other posts from Programmers or by João Melo
Published on 2012-06-19T14:58:52Z Indexed on 2012/06/20 3:23 UTC
Read the original article Hit count: 201

Filed under:
|
|
|

i have a list of users, with minions, something like this:

User52:
       minion10
       minion12
User32:
      minion13
      minion11

i've been keeping in an array where the "location" is the id, like this:

Users:
       [52]User
               minions:
                       [10]minion
                       [12]minion
       [32]User
               minions:
                       [13]minion
                       [11]minion

so i can access them easily like this: user[UserID].minions[MinionID] (ex: user[32].minions[11])
but when i print it or send it by json i get something like this:

{,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,minion,,,,,,,,,,,,,,minion}

but should i keep using like this or should i change to something like this:

User = function(){
   this.minions = ...;
   this.getMinion = function(value){
     for(var m in this.minions){
      if(this.minions[m].id == value){
         return this.minions[m];
         break;
      }
     }
   } 
}

and get it like this:

user.getMinion(MinionID);

Question: i get better performance using a "short" array but using loops every time i need a minion, or using "long" arrays, but no need for loop and getting values directly from the id "name"?

© Programmers or respective owner

Related posts about JavaScript

Related posts about concepts