I was wondering if someone can compare & contrast the differences between redux reselect lib vs lodash memoize...?
compare verb [T] (EXAMINE DIFFERENCES)to examine or look for the difference between two or more things: If you compare house prices in the two areas, it's quite amazing how different they are.
Examples of compare in a Sentence Verb The singer's voice has been compared to that of Elvis. We each did the homework assignment, then compared answers. I compared several bicycles before buying one.
Adjectives and adverbs can be used to make comparisons. The comparative form is used to compare two people, ideas, or things. The superlative form with the word "the" is used to compare three or more. Comparatives and superlatives are often used in writing to hedge or boost language.
Lodash's memoize is a classic memoizing utility: It memoizes a function using (by default) its first argument as cache key. Lodash's memoize keeps track/caches all the results obtained with different cache keys.
Reselect instead, checks each provided argument, and (by default) recomputes the result ONLY IF one of the arguments changes. Reselect's selectors have by default a cache size of 1, and are mainly designed to stabilize state-derived data avoiding undesired recomputations.
const memoizedFunc = _.memoize(
(param1, param2) => param1 + param2
);
console.log(memoizedFunc(1, 2)); // 3
console.log(memoizedFunc(1, 3)); // 3 (but it should be 4!)
Note that you can write a custom resolver
function and pass it in as the second argument to _.memoize
to change this behavior and thus take all or some parameters into account. This is extra logic to test and maintain and may or may not be worth it to you. (The resolver function determines the key to be used in the Map of cached results that the memoized function maintains. By default the key is just set to equal the first parameter.)
Note that reselect
is primarily used for redux
applications, creating memoized selectors with createSelector
. In createSelector
, each parameter is expected to have a getter (selector) function, because of the expectation of usage with a redux
store. If you are not trying to memoize data from a redux
store, you still could use this and send in an identity function like _.identity
for each parameter, but that would be silly.
Fortunately, if you only want to memoize functions, and you want all parameters checked for changes, you can use reselect
's defaultMemoize
and get the desired behavior.
If you want all parameters checked for changes, and are not using redux
or otherwise need to use createSelector
, you may want to just use a very lightweight and fast library intended for specifically for this like memoize-one
.
If you do want createSelector
, you can just use reselect
for all your needs most likely.
If you want all results to be cached, not just the most recent ones, you can use lodash _.memoize
alone, and you can also customize reselect
to use that feature of lodash _.memoize
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With