Skip to content

Instantly share code, notes, and snippets.

@bitfishxyz
Created January 18, 2020 10:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitfishxyz/7ae81909906080886cd17b4f6eb432c8 to your computer and use it in GitHub Desktop.
Save bitfishxyz/7ae81909906080886cd17b4f6eb432c8 to your computer and use it in GitHub Desktop.
function cached(fn){
// Create an object to store the results returned after each function execution.
const cache = Object.create(null);
// Returns the wrapped function
return function cachedFn (str) {
// If the cache is not hit, the function will be executed
if ( !cache[str] ) {
let result = fn(str);
// Store the result of the function execution in the cache
cache[str] = result;
}
return cache[str]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment