memoization
1.0.2 • Public • Published
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;
Package Sidebar
Install
Weekly Downloads