cucurry

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
old
var autoCurry = function(fn, numArgs) {
  numArgs = numArgs || fn.length;
  return function() {
    if (arguments.length < numArgs) {
      return numArgs - arguments.length > 0 ?
        wu.autoCurry(wu.curry.apply(this, [fn].concat(toArray(arguments))),
          numArgs - arguments.length) :
        wu.curry.apply(this, [fn].concat(toArray(arguments)));
    } else {
      return fn.apply(this, arguments);
    }
  };
};

var fun = function(x, y, z) {
  return x + y + z;
};
fun = autoCurry(fun);

for (var i = 0; i < 10000; i++) {
  fun(i)(i + 1)(i + 2);
}
ready
new
var ARR_SLICE = [].slice;
var StopIteration = function() {};

var isInstance = function(obj, Type) {
  return obj instanceof Type;
};

var toArray = function(obj) {
  return ARR_SLICE.call(obj);
};

var curry = function(fn /* variadic number of args */ ) {
  var args = ARR_SLICE.call(arguments, 1);
  return function() {
    return fn.apply(this, args.concat(toArray(arguments)));
  };
};

var autoCurry = function(fn, numArgs) {
  numArgs = numArgs || fn.length;

  return function() {
    if (arguments.length < numArgs) {
      return numArgs - arguments.length > 0 ?
        autoCurry(curry.apply(this, [fn].concat(toArray(arguments))),
          numArgs - arguments.length) :
        curry.apply(this, [fn].concat(toArray(arguments)));
    } else {
      return fn.apply(this, arguments);
    }
  };
};

var notoCurry = function(fn, numArgs) {
  numArgs = numArgs || fn.length;

  var twoArgs = function(one, two) {
    // all arguments
    if (two) return fn(one, two);

    // one argumemt
    else if (one) return function(z) {
      return fn(one, z);
    }

    // no arguments
    else return fn;
  };

  var threeArgs = function(one, two, three) {
    // all arguments
    if (three) return fn(one, two, three);

    // two arguments
    else if (two) return function(z) {
      return fn(one, two, z)
    };

    // one argument
    else if (one) {
      var fun = function(x, y) {
        if (y) return fn(one, x, y);
        else if (x) return function(z) {
          return fn(one, x, z);
        };
        else return fun;
      };
      return fun;
    }

    // no arguments
    else return fn;
  }

  switch (numArgs) {
    case 1:
      return fn;
    case 2:
      return twoArgs;
    case 3:
      return threeArgs;
    default:
      return autoCurry(fn);
  }
};

var fun = function(x, y, z) {
  return x + y + z;
};
fun = notoCurry(fun);

for (var i = 0; i < 10000; i++) {
  fun(i)(i + 1)(i + 2);
}
ready

Revisions

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