y.bind

JavaScript performance comparison

Revision 2 of this test case created by Juan

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);
        };  
    };
   
    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);
   
    var slice = Array.prototype.slice;
   
    var bind2 = function (method, obj) {
        var args = arguments.length > 2 ? slice.call(arguments, 2) : [],
            Noop = function () {},
            fBound = function () {
              var fn = typeof method === 'string' ? obj[method] : method;
   
              return fn.apply(
                this instanceof Noop ? this : obj,
                args.concat(slice.call(arguments))
              );
            };
       
        if (typeof method === 'function') {
          Noop.prototype = method.prototype;
          fBound.prototype = new Noop();
        }
       
        return fBound;
      };
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
old Y.bind
var fn = Ybind(noop, window, 1, 2, 3, 4);
pending…
new Y.bind
var fn = newYbind(noop, window, 1, 2, 3, 4);
pending…
bind2
var fn = bind2(noop, window, 1, 2, 3, 4);
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:

0 comments

Add a comment