Skip to content

Instantly share code, notes, and snippets.

@mape
Created July 18, 2010 18:40
Show Gist options
  • Select an option

  • Save mape/480607 to your computer and use it in GitHub Desktop.

Select an option

Save mape/480607 to your computer and use it in GitHub Desktop.
var userIds = [];
// Create dummy ids
for (var i=0; i < 10000; i++) {
userIds.push('id-'+new Date().getTime()+Math.random()*100000000000000000+''+(Math.random()*100000000000000000)+''+(Math.random()*100000000000000000));
};
// Populate array with ids and users attached
var usersArr = []
for (var i=0,l=users.length; i < l; i++) {
usersArr.push({
'id': userIds[i]
, 'name': 'mape-'+userIds[i]
});
};
// Populate object with ids and users attached
var usersObj = []
for (var i=0,l=users.length; i < l; i++) {
usersObj[userIds[i]] = {
'name': 'mape-'+userIds[i]
}
};
// Generate an id to find
var randomId = userIds[Math.floor(Math.random()*userIds.length)];
console.log('Find: '+randomId);
console.time('Fetch in array')
for (var i=0,l=users.length; i < l; i++) {
if (usersArr[i].id === randomId)
{
console.dir(usersArr[i]);
break;
}
};
console.timeEnd('Fetch in array')
console.time('Fetch in object')
console.dir(usersObj[randomId]);
console.timeEnd('Fetch in object')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment