bind vs emulate (v27)

Revision 27 of this benchmark created by dh on


Description

fixed bug in raynos. tried to make it faster. tried closure compiler version

Preparation HTML

<script>

Function.prototype.bindRaynos = function (context) {
          var f = this;
          var curriedArgs = Array.prototype.slice.call(arguments, 1);
          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 f.apply(context, allArgs);
            };
          } else {
            return createProxy(f, context);
          }
        };

        function createProxy(f, context) {
          return function () {
            return f.apply(context, arguments);
          }
        }

        Function.prototype.bindRaynos2 = function (context) {
          var f = this;
          var curriedArgs = Array.prototype.slice.call(arguments, 1);
          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 f.apply(context, allArgs);
            };
          } else {
            return createProxy(f, context);
          }

        function createProxy(f, context) {
          return function () {
            return f.apply(context, arguments);
          }
        }
        };

        Function.prototype.bindClosure=function(d){function c(b,a){return function(){return b.apply(a,arguments)}}var e=this,f=Array.prototype.slice.call(arguments,1);return f.length?function(){for(var b=f.slice(0),a=0,c=arguments.length;a<c;++a)b.push(arguments[a]);return e.apply(d,b)}:c(e,d)};

        var i = {};
        var f = function () {
          return this.i = Array.prototype.slice.call(arguments, 0).join(',');
        };

        var nativelyBound = f.bind(i);
        var nativelyBoundArgs = f.bind(i, 'xxx');
        var raynosBound = f.bindRaynos(i);
        var raynosBoundArgs = f.bindRaynos(i, 'xxx');
        var raynos2Bound = f.bindRaynos2(i);
        var raynos2BoundArgs = f.bindRaynos2(i, 'xxx');
        var closureBound = f.bindClosure(i);
        var closureBoundArgs = f.bindClosure(i, 'xxx');
        
        console.assert(nativelyBound() === '');
        console.assert(nativelyBoundArgs() === 'xxx');
        console.assert(nativelyBound('xyz') === 'xyz');
        console.assert(nativelyBoundArgs('xyz') === 'xxx,xyz');
        console.assert(raynosBound() === '');
        console.assert(raynosBoundArgs() === 'xxx');
        console.assert(raynosBound('xyz') === 'xyz');
        console.assert(raynosBoundArgs('xyz') === 'xxx,xyz');
        console.assert(raynos2Bound() === '');
        console.assert(raynos2BoundArgs() === 'xxx');
        console.assert(raynos2Bound('xyz') === 'xyz');
        console.assert(raynos2BoundArgs('xyz') === 'xxx,xyz');

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Native version
nativelyBound();
nativelyBound('xxx');
nativelyBoundArgs();
nativelyBoundArgs('xyz');
ready
Raynos's version
raynosBound();
raynosBound('xxx');
raynosBoundArgs();
raynosBoundArgs('xyz');
ready
Raynos 2
raynos2Bound();
raynos2Bound('xxx');
raynos2BoundArgs();
raynos2BoundArgs('xyz');
ready
raynos's version closure compiler vanced
closureBound();
closureBound('xxx');
closureBoundArgs();
closureBoundArgs('xyz');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.