Skip to content

Instantly share code, notes, and snippets.

@rayinla
Last active October 2, 2021 21:18
Show Gist options
  • Save rayinla/2a2460b807a095b124aaca7646ead065 to your computer and use it in GitHub Desktop.
Save rayinla/2a2460b807a095b124aaca7646ead065 to your computer and use it in GitHub Desktop.
//Knowing that we have access to whatever
//the user inputs into our function expression, we then write...
return function(){
var key = JSON.stringify(arguments);
if (cash[key]){
return cache[key];
}
else{
//apply() comes in handy here and will simply
//return the value of the function it calls
val = func.apply(this, arguments);
//then we set the value of the function to the key(argument).
//The next time the function runs,
//if the argument is the same, we simply return
//the value without having to have the function execute.
cash[key] = val;
return val;
}
}
@w3uiguru
Copy link

@rayinla There is typo in above gist

if (cash[key]){} should be
if (cache[key]){}

similarly in else condition. See cash[key] = val;

@saurbhc
Copy link

saurbhc commented Mar 22, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment