1

is declaring an array and initializing some arbitrary indexes allocate all the array elements in the memory even the undefined ones?

Example:

var users = [];

function addUser(userID, name, address) {
   if (typeof (users[userID]) === 'undefined')
       users[userID] = new User(userID, name, address)
}

function User (userID, name, address) {
    this.userID = userID;
    this.name = name;
    this.address = address;
}

$(function () {
   addUser(63, 'John', 'VA');
   addUser(5, 'Kate', 'NY');
   addUser(895, 'Yaz', 'DC');
});

So in the above example, will the browser allocate 896 instances of User in the memory (only 3 are defined) or only 3?

Thanks,

4
  • What browser are you using? Also, did you intend to have a close parentheses and semicolon at the end of the sample code? Commented Feb 13, 2014 at 5:30
  • 2
    Look at youtu.be/UJPdhx5zTaw?t=17m25s for Google Chrome. Commented Feb 13, 2014 at 5:31
  • I'm targeting all the major browsers. I fixed my jQuery syntax error Commented Feb 13, 2014 at 5:32
  • If you do var arr = []; a[1000] = 42; console.dir(a);, then you will see that there is only one element in the array. Commented Feb 13, 2014 at 5:49

1 Answer 1

1

Nope

JavaScript doesn't care what you put in the array, and it's not going to auto-populate it with values you didn't give it.

If you add 3 users to the array, you will only have 3 users in memory.

The indices in the gaps will just be undefined


var x = [];
// undefined

x[0] = "user1";
// 'user1'

x[3] = "user2";
// 'user2'

x[10] = "user3";
// 'user3'

x;
// ['user1',,,'user2',,,,,,,'user3']

All of that said, you might be better off using an Object ({})

var users = {};

function addUser(userID, name, address) {
  if (!(userID in users)) {
    users[userID] = new User(userID, name, address)
  }
}

You will have an object that looks like this

{"63": [object User], "5": [object User], "895": [object User]}
Sign up to request clarification or add additional context in comments.

6 Comments

I couldn't understand the difference. Can you please elaborate more? I can see {} over there, aren't you declaring an array too?
@yazanpro, the first example uses an Array the second example uses an Object. The object won't have "gaps" like the array will. I recommend reading up on both on MDN.
Technically speaking there is no differences between arrays and objects in that case. You say that the gaps are undefined, but that's the same case for objects: non-existing properties are always undefined.
@FelixKling so would you suggest a better approach to achive the purpose?
@yazanpro: Since the you actually have user IDs, an object feels more appropriate. Objects are typically used as key -> value maps whereas arrays are more of a set of elements. And it looks like you rather want a map here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.