Skip to content

Instantly share code, notes, and snippets.

@germancutraro
Last active January 5, 2020 02:52
Show Gist options
  • Save germancutraro/9401c59bf850ff9fb028711bb19fdb01 to your computer and use it in GitHub Desktop.
Save germancutraro/9401c59bf850ff9fb028711bb19fdb01 to your computer and use it in GitHub Desktop.
Easy memorizing function js
const calcs = {};
const sum = (a,b) => {
let key = 'sum' + (a+b).toString();
if ( key in calcs )
return 'found: ' + calcs[key]
let total = a + b;
total--;
total++;
calcs[key] = total;
return total;
}
sum(2, 2)
sum(2, 3)
let memo = sum(2, 3)
console.log(memo)
console.log(calcs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment