Skip to content

Instantly share code, notes, and snippets.

@rayinla
Last active January 13, 2018 20:57
Show Gist options
  • Select an option

  • Save rayinla/3300dbfa85838983e87fb47dabb3bc6a to your computer and use it in GitHub Desktop.

Select an option

Save rayinla/3300dbfa85838983e87fb47dabb3bc6a to your computer and use it in GitHub Desktop.
function memo(func){
var cache = {};
return function(){
var key = JSON.stringify(arguments);
if (cache[key]){
console.log(cache)
return cache[key];
}
else{
val = func.apply(null, arguments);
cache[key] = val;
return val;
}
}
}
var fib = memo(function(n) {
if (n < 2){
return 1;
}else{
console.log("loading...")
return fib(n-2) + fib(n-1);
}
});
fib(7)
fib(6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment