arguments vs apply

Benchmark created on


Setup

const emptyArray = [];
const singleMemoMulti = (fn) => {
  let lastArgs = emptyArray;
  let lastResult;
  return (...args) => {
    if (lastArgs === emptyArray) {
      lastArgs = args;
      lastResult = fn(...args);
      return lastResult;
    }
    for (let i = 0; i < args.length; i++) {
      if (lastArgs[i] !== args[i]) {
        lastResult = fn(...args);
        lastArgs = args;
        return lastResult;
      }
    }
    return lastResult;
  };
};

const run = singleMemoMulti((a, b, c, d, e, f, g) => {
})

const singleMemoMultiApply = (fn) => {
  let lastArgs;
  let lastResult;
  return function() {
    if (!lastArgs) {
      lastArgs = arguments;
      lastResult = fn.apply(null, arguments)
      return lastResult;
    }
    for (let i = 0; i < arguments.length; i++) {
      if (lastArgs[i] !== arguments[i]) {
        lastResult = fn.apply(null, arguments);
        lastArgs = arguments;
        return lastResult;
      }
    }
    return lastResult;
  };
};

const runApply = singleMemoMultiApply((a, b, c, d, e, f, g) => {})

Test runner

Ready to run.

Testing in
TestOps/sec
singleMemoMulti
run(1, [], 'str', 4, {}, window)
ready
singleMemoMultiApply
runApply(1, [], 'str', 4, {}, window)
ready

Revisions

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