Currying methods

JavaScript performance comparison

Test case created by Carlos Vázquez

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.23.0/ramda.min.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  function curryFor(f) {
  	var self = this;
  
  	return function() {
  		if (arguments.length >= f.length) {
  			return f.apply(self, arguments);
  		} else {
  			for (var g = f, i = 0, le = arguments.length; i < le; i++) {
  				g = g.bind(self, arguments[i]);
  			}
  			return curry(g);
  		}
  	};
  };
  
  function curry(f) {
  	var self = this;
  
  	return function() {
      var al = arguments.length;
  		if (al >= f.length) {
  			return f.apply(self, arguments);
  		} else {
        g = f;
        g = g.bind(self, Array.prototype.slice.call(arguments, al));
  			return curry(g);
  		}
  	};
  };

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Hand made (function on the fly)
addHandMadeFly = function(a, b) {
    if (b != undefined) {
        return a + b;
    }
    return function(c) {
        return a + c;
    }
}
pending…
Custom curryFor
addCustomCurry = curryFor(function(a, b) {
    return a + b;
});
pending…
Ramda curry
addRamdaCurry = R.curry(function(a, b) {
    return a + b;
});
pending…
Hand made (binding other function)
_addHandMadeBinding = function(a, b) {
    return a + b;
}

addHandMadeBinding = function(a, b) {
    if (b != undefined) {
        return a + b;
    } else {
        return _addHandMadeBinding.bind(this, a)
    }
}
pending…
Custom curry
addCustomCurry = curry(function(a, b) {
    return a + b;
});
pending…
Hand made automatic
_addHandMadeAuto = function(a, b) {
    return a + b;
}

addHandMadeAuto = function(a, b) {
    if (b != undefined) {
        return _addHandMadeAuto(a, b);
    } else {
        return function(c) {
           _addHandMadeAuto(a, c)
        }
    }
}
pending…

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

0 Comments