Skip to content

Instantly share code, notes, and snippets.

@dpedro
Last active August 10, 2016 15:30
Show Gist options
  • Save dpedro/6075c756e9ec839075812ba81744165c to your computer and use it in GitHub Desktop.
Save dpedro/6075c756e9ec839075812ba81744165c to your computer and use it in GitHub Desktop.
Array by key, value
var users = [];
users['john'] = { id: 'john01', role: 'admin' };
users['bob'] = { id: 'bob01', role: 'reader' };
console.log(users);
console.log(users['bob']);
console.log(Object.keys(users).length)
/*
-- DOC --
var users = new Array();
new Array requires a name lookup on Array (you can have a variable with that name in scope), whereas [] does not.
The relatively small difference in performance supports this point I think. You could do the same test with the Object and object literal {} by the way.
users.length, gives you the next index available in the array.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment