Created
August 10, 2021 14:23
-
-
Save luan0ap/356b062763b1fb75238be26fbe269e45 to your computer and use it in GitHub Desktop.
memoizer using es6
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 memoizer (fn, initialState = null) { | |
const _cache = initialState || {} | |
return (...params) => { | |
const _stringified = JSON.stringify(params) | |
if (_cache[_stringified] === undefined) { | |
const result = fn(...params) | |
_cache[_stringified] = result | |
return result | |
} | |
return _cache[_stringified] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment