Skip to content

Instantly share code, notes, and snippets.

@samcday
Created February 21, 2011 04:50
Show Gist options
  • Save samcday/836669 to your computer and use it in GitHub Desktop.
Save samcday/836669 to your computer and use it in GitHub Desktop.
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