y.bind
JavaScript performance comparison
Info
https://github.com/yui/yui3/pull/320
Preparation code
<script src="http://yui.yahooapis.com/3.9.0pr1/yui/yui-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
var Y = YUI(),
L = Y.Lang,
noop = function() {},
Ybind = function(f, c) {
var xargs = arguments.length > 2 ?
Y.Array(arguments, 2, true) : null;
return function() {
var fn = L.isString(f) ? c[f] : f,
args = (xargs) ?
xargs.concat(Y.Array(arguments, 0, true)) : arguments;
return fn.apply(c || fn, args);
};
};
var slice = Array.prototype.slice;
function bind_(r,f,c){
var xargs = arguments.length > 3 ?
Y.Array(arguments, 3, true) : [],
Noop = function () {
},
bound = function () {
var fn = L.isString(f) ? c[f] : f,
inArgs = Y.Array(arguments, 0, true);
return fn.apply(this instanceof Noop ? this : c,
(r ? inArgs.concat(xargs) : xargs.concat(inArgs)));
};
if (L.isFunction(f)) {
Noop.prototype = f.prototype;
bound.prototype = new Noop();
}
return bound;
}
var newYbind = bind_(0, bind_, null, 0);
function bindLight(method, obj) {
// bind the method once, so the actual function
// being bound does not change
if (typeof method === 'string') {
method = obj[method];
}
if (typeof method !== 'function') Y.log('Y.bind - what is trying to be bound is not callable', 'error');
var args = slice.call(arguments, 2),
Noop = function () {},
bound = function () {
return method.apply(
// Allow the bound function to be called with "new"
// In that case |this| will be a new object instace of Noop
this instanceof Noop ? this : obj,
args.concat(slice.call(arguments))
);
};
Noop.prototype = method.prototype;
bound.prototype = new Noop();
return bound;
}
function nativeBind(method, obj) {
if (typeof method === 'string') {
method = obj[method];
}
return method.bind(obj);
}
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
old Y.bind |
|
pending… |
new Y.bind |
|
pending… |
light Y.bind |
|
pending… |
native bind |
|
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 Dav Glass
- Revision 2: published by Juan
- Revision 3: published by Juan
0 comments