memoization

Straightforward implementation of memoization.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
memoization
611.0.26 years ago7 years agoMinified + gzip package size for memoization in KB

Readme

Build Status
memoization
Straightforward implementation of memoization in javascript

Exemple Usage

var memoize = require("memoization");
var fib = memoize(function(n){return n<2 ? n : fib(n-1) + fib(n-2);});

Code

function memoize(f) {
  var dict = {};
  return function() {
    var args = JSON.stringify(arguments);
    if(dict.hasOwnProperty(args)) return dict[args];
    var res = f.apply(this, arguments);
    dict[args] = res;
    return res;
  }
}

if (typeof module === "object") module.exports = memoize;