wat

Benchmark created on


Preparation HTML

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

  return function (...args) {
    const key = resolver ? resolver.apply(this, args) : args[0];

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

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

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

    return result;
  };
}

function memoizeWeakMap(func, resolver) {
  let cache = new WeakMap();

  return function (...args) {
    const key = resolver ? resolver.apply(this, args) : args[0];

    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);
  };
}

const obj = { foo: 12, baz: 12 };

</script>

Setup


Test runner

Ready to run.

Testing in
TestOps/sec
using memoize
memoizeMap(Object.entries)(obj);
ready
using simons memoize
memoizeSimon(Object.entries)(obj);
ready
using weakmap
memoizeWeakMap(Object.entries)(obj);
ready

Revisions

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