Created
January 18, 2020 10:30
-
-
Save bitfishxyz/7ae81909906080886cd17b4f6eb432c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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