Last active
January 21, 2016 14:46
-
-
Save AutoSponge/eed990b3f02c523a025f to your computer and use it in GitHub Desktop.
memoize that allows functions and understands context
This file contains 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
import {List} from 'immutable' | |
export default fn => { | |
const cache = {} | |
const cacheKey = List() | |
return function (...args) { | |
const hashCode = cacheKey.push(this, ...args).hashCode() | |
if (!(hashCode in cache)) { | |
cache[hashCode] = fn.apply(this, args) | |
} | |
return cache[hashCode] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment