bind
JavaScript performance comparison
Preparation code
<script>
Array.slice = Array.from = function(array, start, end){
return Array.prototype.slice.call(array, start, end);
};
Function.prototype.bind1 = function(bind){
var self = this,
args = (arguments.length > 1) ? Array.slice(arguments, 1) : null;
return function(){
if (!args && !arguments.length) return self.call(bind);
if (args && arguments.length) return self.apply(bind, args.concat(Array.from(arguments)));
return self.apply(bind, args || arguments);
};
};
Function.prototype.bind2 = function(that){
var self = this,
args = arguments.length > 1 ? Array.slice(arguments, 1) : null;
var bound = function(){
var bind, F;
if (this instanceof bound){
F = function(){};
F.prototype = self.prototype;
bind = new F;
} else bind = that;
var result = (!args && !arguments.length) ? self.call(bind)
: (args && arguments.length) ? self.apply(bind, args.concat(Array.from(arguments)))
: self.apply(bind, args || arguments);
return F ? bind : result;
};
return bound;
};
Function.prototype.bind3 = function(that){
var self = this,
args = arguments.length > 1 ? Array.slice(arguments, 1) : null,
F = function(){};
var bound = function(){
var context = that, length = arguments,length;
if (this instanceof bound){
F.prototype = self.prototype;
context = new F;
}
var result = (!args && !length)
? self.call(context)
: self.apply(context, args && length ? args.concat(Array.slice(arguments)) : args || arguments);
return context == that ? result : context;
};
return bound;
};
Function.prototype.bind4 = function(that){
var self = this,
args = arguments.length > 1 ? Array.slice(arguments, 1) : null;
var bound = function(){
var context = that, length = arguments,length;
if (this instanceof bound){
var F = function(){};
F.prototype = self.prototype;
context = new F;
}
var result = (!args && !length)
? self.call(context)
: self.apply(context, args && length ? args.concat(Array.slice(arguments)) : args || arguments);
return context == that ? result : context;
};
return bound;
};
(function(global){
var F = function(){};
Function.prototype.bind5 = function(that){
var self = this,
args = arguments.length > 1 ? Array.slice(arguments, 1) : null;
return function(){
var context = that, length = arguments,length;
if (this && this != global){
F.prototype = self.prototype;
context = new F;
}
var result = (!args && !length)
? self.call(context)
: self.apply(context, args && length ? args.concat(Array.slice(arguments)) : args || arguments);
return context == that ? result : context;
};
};
})(this);
Function.prototype.bind6 = function (bind) {
var self = this;
return function () {
var args = Array.prototype.slice.call(arguments);
return self.apply(bind || null, args);
};
};
</script>
<script>
Benchmark.prototype.setup = function() {
var fn = function(type){
this.type = type;
};
var obj = {type: 'bar'};
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Native |
|
pending… |
Old |
|
pending… |
New |
|
pending… |
Improved |
|
pending… |
lazy F |
|
pending… |
better |
|
pending… |
test 7 |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by arian
- Revision 2: published
- Revision 3: published by John-David Dalton
- Revision 4: published by Olmo
- Revision 5: published by arian
- Revision 6: published
0 comments