Skip to content

Instantly share code, notes, and snippets.

@mistidoi
Last active January 26, 2016 05:43
Show Gist options
  • Save mistidoi/f5c90a2497ead818bf4c to your computer and use it in GitHub Desktop.
Save mistidoi/f5c90a2497ead818bf4c to your computer and use it in GitHub Desktop.
putting it to use!
var memoize;
memoize = function(func) {
var cache;
cache || (cache = {});
return function() {
var key;
key = JSON.stringify(arguments);
if (cache[key]) {
return cache[key];
} else {
return cache[key] = func.apply(this, arguments);
}
};
};
add3Numbers = function (a,b,c) { console.log("I just ran!"); return a + b + c; };
add3Numbers(1,2,3);
// I just ran!
// 5
memoizedAdd3Numbers = memoize(add3Numbers);
memoizedAdd3Numbers(3,4,5);
// I just ran!
// 12
memoizedAdd3Numbers(3,4,5);
// 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment