Last active
October 13, 2015 05:17
-
-
Save lsm/4144679 to your computer and use it in GitHub Desktop.
Benchmark between different implementation of javascript 'class'
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
# npm install genji benchmark | |
var genji = require('genji'); | |
var Benchmark = require('benchmark'); | |
var Base = genji.Base; | |
var util = require('util'), | |
times = 500000; | |
console.log('Testing class definition: '); | |
var suite = new Benchmark.Suite; | |
// add tests | |
suite.add('prototype', function() { | |
var User = function(name) { | |
if (name && typeof name === 'string') { | |
this.name = name.trim(); | |
} | |
}; | |
User.prototype.auth = function(username, password) { | |
}; | |
var Admin = function(name) { | |
User.call(this, name); | |
}; | |
Admin.prototype = new User; | |
Admin.prototype.constructor = Admin; | |
}) | |
.add('sys#inherits', function() { | |
var User = function(name) { | |
if (name && typeof name === 'string') { | |
this.name = name.trim(); | |
} | |
}; | |
User.prototype.auth = function(username, password) { | |
}; | |
var Admin = function(name) { | |
User.call(this, name); | |
}; | |
util.inherits(Admin, User); | |
}) | |
.add('genji#base', function() { | |
var User = Base(function(name) { | |
if (name && typeof name === 'string') { | |
this.name = name.trim(); | |
} | |
}, { | |
auth: function(username, password) { | |
} | |
}); | |
var Admin = User({ | |
init: function(name) { | |
this._super(name); | |
} | |
}); | |
}) | |
// add listeners | |
.on('cycle', function(event, bench) { | |
console.log(String(bench)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
// run async | |
.run({ 'async': false }); | |
console.log('Testing class initialization: '); | |
var suite2 = new Benchmark.Suite; | |
// prototype | |
var UserPrototype = function(name) { | |
if (name && typeof name === 'string') { | |
this.name = name.trim(); | |
} | |
}; | |
UserPrototype.prototype.auth = function(username, password) { | |
}; | |
var AdminPrototype = function(name) { | |
UserPrototype.call(this, name); | |
}; | |
AdminPrototype.prototype = new UserPrototype; | |
AdminPrototype.prototype.constructor = AdminPrototype; | |
// sys#inherits | |
var UserSys = function(name) { | |
if (name && typeof name === 'string') { | |
this.name = name.trim(); | |
} | |
}; | |
UserSys.prototype.auth = function(username, password) { | |
}; | |
var AdminSys = function(name) { | |
UserSys.call(this, name); | |
}; | |
util.inherits(AdminSys, UserSys); | |
// genji#base | |
var UserBase = Base(function(name) { | |
if (name && typeof name === 'string') { | |
this.name = name.trim(); | |
} | |
}, { | |
auth: function(username, password) { | |
} | |
}); | |
var AdminBase = UserBase({ | |
init: function(name) { | |
this._super(name); | |
} | |
}); | |
// add tests | |
suite2.add('prototype', function() { | |
new AdminPrototype('zir'); | |
}) | |
.add('sys#inherits', function() { | |
new AdminSys('zir'); | |
}) | |
.add('genji#base', function() { | |
new AdminBase('zir'); | |
}) | |
// add listeners | |
.on('cycle', function(event, bench) { | |
console.log(String(bench)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
// run async | |
.run({ 'async': false }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment