Created
May 12, 2010 06:23
-
-
Save hallettj/398273 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 times = 500000, | |
benchmarks = {}, | |
objects, | |
sys = { | |
puts: function() { console.log.apply(console, arguments); } | |
}; | |
function bm(label, fn) { | |
benchmarks[label] = fn; | |
}; | |
function run(label) { | |
// Keep references to constructed objects so that garbage collection does not | |
// skew the numbers. | |
objects = []; | |
var start = +new Date | |
benchmarks[label](); | |
sys.puts(' ' + label + ' : ' + (+new Date - start) + ' ms') | |
}; | |
bm('dynamic',function(){ | |
var n = times; | |
function A(){ | |
this.method = function(){ return 5; } | |
} | |
while (n--) { | |
objects.push( new A() ); | |
} | |
}); | |
bm('prototype',function(){ | |
var n = times; | |
function A(){} | |
A.prototype.method = function(){ return 5; } | |
while (n--) { | |
objects.push( new A() ); | |
} | |
}) | |
bm('dynamic 10',function(){ | |
var n = times; | |
function A(){ | |
this.method1 = function(){ return 1; } | |
this.method2 = function(){ return 2; } | |
this.method3 = function(){ return 3; } | |
this.method4 = function(){ return 4; } | |
this.method5 = function(){ return 5; } | |
this.method6 = function(){ return 6; } | |
this.method7 = function(){ return 7; } | |
this.method8 = function(){ return 8; } | |
this.method9 = function(){ return 9; } | |
this.method10 = function(){ return 10; } | |
} | |
while (n--) { | |
objects.push( new A() ); | |
} | |
}); | |
bm('prototype 10 methods (obj)',function(){ | |
var n = times; | |
function A(){} | |
A.prototype = { | |
method1 : function(){ return 1; }, | |
method2 : function(){ return 2; }, | |
method3 : function(){ return 3; }, | |
method4 : function(){ return 4; }, | |
method5 : function(){ return 5; }, | |
method6 : function(){ return 6; }, | |
method7 : function(){ return 7; }, | |
method8 : function(){ return 8; }, | |
method9 : function(){ return 9; }, | |
method10 : function(){ return 10; } | |
} | |
while (n--) { | |
objects.push( new A() ); | |
} | |
}) | |
bm('prototype 10 methods (items)',function(){ | |
var n = times | |
function A(){} | |
A.prototype.method1 = function(){ return 1; } | |
A.prototype.method2 = function(){ return 2; } | |
A.prototype.method3 = function(){ return 3; } | |
A.prototype.method4 = function(){ return 4; } | |
A.prototype.method5 = function(){ return 5; } | |
A.prototype.method6 = function(){ return 6; } | |
A.prototype.method7 = function(){ return 7; } | |
A.prototype.method8 = function(){ return 8; } | |
A.prototype.method9 = function(){ return 9; } | |
A.prototype.method10 = function(){ return 10; } | |
while (n--) { | |
objects.push( new A() ); | |
} | |
}) | |
bm('prototype 10 methods (items alt)',function(){ | |
var n = times; | |
function A(){} | |
a = A.prototype; | |
a.method1 = function(){ return 1; } | |
a.method2 = function(){ return 2; } | |
a.method3 = function(){ return 3; } | |
a.method4 = function(){ return 4; } | |
a.method5 = function(){ return 5; } | |
a.method6 = function(){ return 6; } | |
a.method7 = function(){ return 7; } | |
a.method8 = function(){ return 8; } | |
a.method9 = function(){ return 9; } | |
a.method10 = function(){ return 10; } | |
while (n--) { | |
objects.push( new A() ); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment