Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 1.26 KB

MEMOIZE.md

File metadata and controls

41 lines (31 loc) · 1.26 KB
title excerpt
Memoize
an optimization used to speed up consecutive function calls by caching the result of calls with identical input

Memoize

An optimization used to speed up consecutive function calls by caching the result of calls with identical input.

Here is an example of a memoizer function, taken from the book JavaScript The Good Parts by Douglas Crockford, that caches the results from a fibonacci number generator function:

var memoizer = function (memo, formula) {
  var recur = function (n) {
    var result = memo[n];
      if (typeof result !== 'number') {
        result = formula(recur, n);
        memo[n] = result;
      }
      return result;
  };
  return recur;
};

var fibonacci = memoizer([0, 1], function (recur, n) {
  return recur(n  1) + recur(n  2);
});

References

NPM Memoize Modules