Created
February 21, 2011 04:50
-
-
Save samcday/836669 to your computer and use it in GitHub Desktop.
This file contains 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 benchmark = function(fn) { | |
var startTime, time, i, times = []; | |
for(i = 0; i < 1000; i++) { | |
startTime = new Date().getTime(); | |
fn(); | |
times.push(new Date().getTime() - startTime); | |
} | |
var total = 0; | |
times.forEach(function(number) { total += number; }); | |
return total / times.length; | |
}; | |
var myfunc = function() { | |
this.test = 123; | |
}; | |
var viaNew = function() { | |
var objs = []; | |
for(var i = 0; i < 100000; i++) { | |
objs.push(new myfunc()); | |
} | |
}; | |
var viaInitializer = function() { | |
var objs = []; | |
for(var i = 0; i < 100000; i++) { | |
objs.push({ | |
test: 123 | |
}); | |
} | |
}; | |
console.log("took " + benchmark(viaNew) + "ms to create objects via new()"); | |
console.log("took " + benchmark(viaInitializer) + "ms to create objects via object initializer"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment