Skip to content

Instantly share code, notes, and snippets.

@luan0ap
Created August 10, 2021 14:23
Show Gist options
  • Save luan0ap/356b062763b1fb75238be26fbe269e45 to your computer and use it in GitHub Desktop.
Save luan0ap/356b062763b1fb75238be26fbe269e45 to your computer and use it in GitHub Desktop.
memoizer using es6
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