Skip to content

Instantly share code, notes, and snippets.

@nw
Created May 11, 2010 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nw/396836 to your computer and use it in GitHub Desktop.
Save nw/396836 to your computer and use it in GitHub Desktop.
var sys = require('sys'),
times = 500000
function bm(label, fn) {
var start = +new Date
fn()
sys.puts(' ' + label + ' : ' + (+new Date - start) + ' ms')
};
bm('dynamic',function(){
var n = times;
function A(){
this.method = function(){ return 5; }
}
while (n--) {
new A();
}
});
bm('prototype',function(){
var n = times;
function A(){}
A.prototype.method = function(){ return 5; }
while (n--) {
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--) {
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--) {
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--) {
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--) {
new A();
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment