A memoization algorithm that only caches the result of the latest set of arguments, where argument equality is determined via a provided equality function.
npm install @essentials/memoize-onenpm i @essentials/memoize-one
A memoization algorithm that only caches the result of the latest set of arguments, where argument equality is determined via a provided equality function.
The default areEqual function is a strict equality check on the first four arguments provided.
``js
import memoOne from '@essentials/memoize-one'
const toUpper = memoOne(
(value) => value.toUpperCase(),
// are equal?
(args, prevArgs) => args[0] === prevArgs[0]
)
toUpper('foo')
// FOO (uncached)
toUpper('foo')
// FOO (cached)
toUpper('foobar')
// FOOBAR (uncached)
toUpper('foo')
// FOO (uncached)
`
| Argument | Description |
| -------- | ------------------------------------------------------------------------------- |
| fn | The function you're memoizing the arguments and result of |
| areEqual | An equality function. Return true if the arguments are equal, false` if not. |
MIT