Compact memoization comparisons (v4)

Revision 4 of this benchmark created by Nick McCready on


Description

A comparison of compact memoization routines providing the bare minimum functionality necessary for caching.

Preparation HTML

<script src="http://underscorejs.org/underscore-min.js"></script>
<script>
window.underscore = window._;
delete window._;
console.info("underscore is: " + window.underscore);
</script>

<script src="https://rawgithub.com/lodash/lodash/2.4.1/dist/lodash.min.js"></script>
<script>
window.lodash = window._;
delete window._;
console.info("lodash is: " + window.lodash);
</script>

<script src="http://rawgit.com/lodash/lodash/master/dist/lodash.min.js"></script>
<script>
window.lodashLatest = window._;
delete window._;
console.info("lodashLatest is: " + window.lodashLatest);
</script>

<script>
var fib = function(x) {
  if (x < 2) return 1;
  else return fib(x - 1) + fib(x - 2);
}

// compact memoizer
function memoize1(param) {
  this.memoize = this.memoize || {};
  return (param in this.memoize) ? this.memoize[param] : this.memoize[param] = fib(param);
}

// using an external cache
var memCache = {};

function memoize2(param, cache) {
  cache = cache || {};
  return (param in cache) ? cache[param] : cache[param] = fib(param);
}


// stoyan's memoizer
function memoize3(param) {
  if (!memoize3.cache) {
    memoize3.cache = {};
  }
  if (!memoize3.cache[param]) {
    var result = fib(param); //custom function
    memoize3.cache[param] = result;
  }
  return memoize3.cache[param];
}

//latest gratest sept 9th 2014 memoize.js
var memoize4 = function(func) {
  var stringifyJson = JSON.stringify,
    cache = {};

  var cachedfun = function() {
    var hash = stringifyJson(arguments);
    return (hash in cache) ? cache[hash] : cache[hash] = func.apply(this, arguments);
  };

  cachedfun.__cache = (function() {
    cache.remove || (cache.remove = function() {
      var hash = stringifyJson(arguments);
      return (delete cache[hash]);
    });
    return cache;
  }).call(this);

  return cachedfun;
};

var memoize5 = underscore.memoize;

var memoize6 = lodash.memoize;

var memoize7 = lodashLatest.memoize;

var m1, m2, m3, m4, m5, m6, m7 = 0;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Compact memoize.js
m1 = memoize1(10);

setTimeout(function() {
  m1 = memoize1(10);
}, 1000);
ready
Stoyan's version
m3 = memoize3(10);

setTimeout(function() {
  m3 = memoize3(10);
}, 1000);
ready
With external cache
m2 = memoize2(10, memCache);

setTimeout(function() {
  m2 = memoize2(10, memCache);
}, 1000);
ready
memoize.js (fn)
m4 = memoize4(fib);
m4(10);
setTimeout(function() {
  m4(10);
}, 1000);
ready
underscore (fn)
m5 = memoize5(fib);
m5(10);
setTimeout(function() {
  m5(10);
}, 1000);
ready
lodash (fn)
m6 = memoize6(fib);
m6(10);
setTimeout(function() {
  m6(10);
}, 1000);
ready
lodash latest (fn)
m7 = memoize7(fib);
m7(10);
setTimeout(function() {
  m7(10);
}, 1000);
ready

Revisions

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

  • Revision 1: published by Addy Osmani on
  • Revision 2: published on
  • Revision 3: published by Nick McCready on
  • Revision 4: published by Nick McCready on
  • Revision 5: published by yespeter on