Last active
August 10, 2016 15:30
-
-
Save dpedro/6075c756e9ec839075812ba81744165c to your computer and use it in GitHub Desktop.
Array by key, value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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