Created
May 29, 2012 10:11
-
-
Save jonelf/2823871 to your computer and use it in GitHub Desktop.
Performance test of searching in array of dynamic objects
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
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