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 = new self;
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(bind){
var self = this, args = Array.slice(arguments), args = (args.length > 1) ? args.slice(1) : null;
return function bound(){
if (!this instanceof bound) return bind;
var F = function(){};
F.prototype = new self;
bind = new F;
args = args.concat(Array.slice(arguments));
return !args ? self.call(bind) : self.apply(bind, args);
};
};
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… |
New 2 |
|
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