bind vs emulated bind vs simpler bind

JavaScript performance comparison

Revision 25 of this test case created by Avi Kohn

Preparation code

<script>
        var scope = {
                num: 2
        };

        var fn = function() {
                return this.num + 1;
        };

        var emulateBind = function (fn, context) {
                var curriedArgs = Array.prototype.slice.call(arguments, 2);

                if (curriedArgs.length) {
                        return function () {
                                var allArgs = curriedArgs.slice(0);

                                for (var i = 0, n = arguments.length; i < n; ++i) {
                                        allArgs.push(arguments[i]);
                                }

                                return fn.apply(context, allArgs);
                        };
                }
                else {
                        return createProxy(fn, context);
                }
        };

        var createProxy = function(fn, context) {
                return function() {
                        return fn.apply(context, arguments);
                }
        }

        var simpleBind = function(fn, ctx) {
                return function() {
                        return fn.apply(ctx, arguments);
                };
        };
</script>
<script>
Benchmark.prototype.setup = function() {
    var bound = fn.bind(scope),
        simple = simpleBind(fn, scope),
        emulated = emulateBind(fn, scope),
        direct = function() {
                return scope.num + 1;
        };
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Native bind
bound();
pending…
Emulated bind
emulated();
pending…
Simple bind
simple();
pending…
Direct calling of scope
direct();
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