Skip to content

Instantly share code, notes, and snippets.

@peterpme
Created March 4, 2014 20:46
Show Gist options
  • Save peterpme/9355268 to your computer and use it in GitHub Desktop.
Save peterpme/9355268 to your computer and use it in GitHub Desktop.
Erik's Memoization Example with ternary operators
function addMe(num){
return num+num;
}
function memoMe(fn){
var objs = {};
return function(x){
return (objs[x]) ? objs[x] : (objs[x] = fn.apply(void 0, arguments), objs[x]);
};
}
var test = memoMe(addMe);
console.log(test(5));
console.log(test(5));
console.log(test(10));
console.log(test(6));
console.log(test(6));
console.log(test(10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment