wat3

Benchmark created on


Preparation HTML

<script>
function memoizeMap(func, resolver) {
  let cache = new Map();

  return function (...args) {
    const key = JSON.stringify(args);

    if (cache.has(key)) {
      return cache.get(key);
    }

    const result = func.apply(this, args);

    cache = cache.set(key, result) || cache;

    return result;
  };
}

function memoizeSimon(fn) {
  const memo = new Map();
  return function (...x) {
    const key = JSON.stringify(x);
    return memo.set(key, memo.has(key) ? memo.get(key) : fn(...x)).get(key);
  };
}

function memoizeObject(fn) {
  const memo = {};
  return function (...x) {
    const key = JSON.stringify(x);
    const result = key in memo ? memo[key] : undefined;
    if (result) {
    	return result;
    }
    return (memo[key] = fn(...x));
  };
}

const obj = { foo: 12, baz: 12 };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
using lodash memoize
memoizeMap(Object.entries)(obj);
ready
using simons memoize
memoizeSimon(Object.entries)(obj);
ready
using memoize with object
memoizeObject(Object.entries)(obj);
ready

Revisions

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