within contexts like react and redux where immutability is a key concern, and where expensive events like rendering are triggered when strict object equality is broken, it would be nice to keep the previous value whenever possible. This is especially true if repeated calculations are likely to generate the same results (but housed in a new container). This is NOT for use with async functions/promises.
npm install --save memoize-rv
const mem = ;
returns a function
memFunc that can be used in place of
memFunc is first evaluated, the return value is stored. Upon repeated evaluation, if the return value is deemed equal to the stored value, then the new value is simply discarded, and the stored value is both kept and returned in place of the new value. The stored value is initialized to undefined.
The default equality check is simply
(rv, stored) => rv === stored, but a custom 'equality check' function is the optional second argument.
Given a function
func of some sort
const func = a b;
This creates a memoized version of
func, with default
const memFunc = ;const val1 = ; // [1, 2]const val2 = ; // [1, 2]// howeverconst areDifferent = val1 === val2; // false
But if we instead look at the equality of the components with a custom equality operator
const eq = a && b && a0 === b0 && a1 === b1;const memFuncEq = ;const valEq1 = ; // [1, 2]const valEq2 = ; // [1, 2]// now they are evaluated equalconst areSame = valEq1 === valEq2; // true
memoize-rv is under the MIT license. See license file.