Skip to content

Instantly share code, notes, and snippets.

@jonelf
Created May 29, 2012 10:11
Show Gist options
  • Save jonelf/2823871 to your computer and use it in GitHub Desktop.
Save jonelf/2823871 to your computer and use it in GitHub Desktop.
Performance test of searching in array of dynamic objects
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var houses = [];
for(var i = 0; i < 10000; i++) {
var house = {
Street:"Orchard Street",
StreetNumber:getRandomInt(1,20),
City:"Gotham",
};
if (getRandomInt(1,3)>1)
house.Fiber = true;
if (getRandomInt(1,7)>5)
house.Bank = "JP Morgan";
houses.push(house);
}
function search(){
var found = 0;
for(var i = 0; i < houses.length; i++) {
var house = houses[i];
if (house.Street == "Orchard Street" &&
house.StreetNumber > 3 && house.Fiber == true) {
found++;
}
}
return found;
}
var startTime = new Date().getTime();
found = 0;
for(var i = 0; i < 100; i++) {
found += search();
}
console.log(new Date().getTime()-startTime);
console.log("Found: " + found);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment