function.curry

JavaScript performance comparison

Test case created by Eric Siebeneich

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var nums = [];
  for(var i = 0; i < 4; i++) {
    nums.push(Math.random() * 100000 - 50000);
  }
  
  function f1(a, b, c, d) {
    return a + b + c + d;
  }
  
  function f2(a, b) {
    return function(c, d) {
      f1(a, b, c, d);
    }
  }
  var f2created = f2(nums[0], nums[1]);
  
  function f3(a, b) {
    var func = f1.bind(null, a, b);
    return function(c, d) {
      return func(c, d);
    }
  }
  var f3created = f3(nums[0], nums[1]);
  
  Function.prototype.curry = function() {
    var func = this;
  	var args = Array.prototype.slice.call(arguments);
  	return function() {
  		return func.apply(this, args.concat(Array.prototype.slice.call(arguments)));
  	};
  };
  var f4 = f1.curry(nums[0], nums[1]);
  
  (function() {
          var funcDecs = {};
  
          function funcCall(cArgs, fArgs) {
                  funcDecs[cArgs] = funcDecs[cArgs] || {};
                  if (!funcDecs[cArgs][fArgs]) {
                          var cArr = [];
                          for (var i = 0; i < cArgs; i++) {
                                  cArr.push('cArgs[' + i + ']');
                          }
                          var cArgStr = cArr.join(', ');
                          var fArr = [];
                          for (var i = 0; i < fArgs; i++) {
                                  fArr.push('fArgs[' + i + ']');
                          }
                          var fArgStr = fArr.join(', ');
                          var evalStr =
                          "[function(_this, func, cArgs, fArgs) {" +
                                  "return func.call(_this" +
                                          (cArgs ? ", " + cArgStr : '') +
                                          (fArgs ? ", " + fArgStr : '') + ");" +
                          "}]";
                          funcDecs[cArgs][fArgs] = eval(evalStr)[0];
                  }
  
                  return funcDecs[cArgs][fArgs];
          }
  
          Function.prototype.curryCall = function() {
                  var func = this;
                  var args = Array.prototype.slice.call(arguments);
                  var aLen = args.length;
                  return function() {
                          return (funcDecs[aLen] && funcDecs[aLen][arguments.length] || funcCall(aLen, arguments.length))(this, func, args, Array.prototype.slice.call(arguments));
                  }
          };
  })();
  var f5created = f1.curryCall(nums[0], nums[1]);
  var f5warmed = f1.curryCall(nums[0], nums[1], nums[2]);
  f5warmed(nums[3]);

};
</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
f1
f1(nums[0], nums[1], nums[2], nums[3]);
pending…
f2
f2(nums[0], nums[1])(nums[2], nums[3]);
pending…
f3
f3(nums[0], nums[1])(nums[2], nums[3]);
pending…
curry
f1.curry(nums[0], nums[1])(nums[2], nums[3]);
pending…
f2created
f2created(nums[2], nums[3]);
pending…
f3created
f3created(nums[2], nums[3]);
pending…
f4
f4(nums[2], nums[3]);
pending…
f5created
f5created(nums[2], nums[3]);
pending…
f5
f1.curryCall(nums[0], nums[1])(nums[2], nums[3])
pending…
f5warmed
f5warmed(nums[3]);
pending…

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

0 Comments