However, one way that you can easily optimize a function is by caching values that frequently rendering components use. This is exactly what React’s useMemo and reselect do to improve performance.
Using useMemo
useMemo is a React hook that memoizes a function’s return value by taking two parameters: a function and a dependency array.
const value = useMemo(() => expensiveFunction(paramA, paramB), [paramA, paramB]);
It works by remembering the value returned based on the dependencies you pass --- normally the function’s parameters --- and returning that value if it was already processed and still cached in memory.
But when is the right time to use this? Should you spam it on every function that might be a tad complex? No, caching and evaluating dependencies still takes CPU time and memory for caching the values, especially if they’re large or frequently changing. This could cause no performance difference at all or make it even worse. You should only use this in cases where performance is noticeably impacted, otherwise, you could just end up hindering performance rather than improving it.
How Redux helps optimize performance by preventing rerenders
Redux’s mapStateToProps gets called every time a dispatch happens, so they’re frequently called. It uses the results of these calls to determine whether or not a rerender needs to happen based on doing a shallow check on the returned props vs the previous props. This leads us to two things. First, make sure your selectors are consistent and not prone to changing when unnecessary to prevent rerenders. Second, your selectors and any other calculations you do in mapStateToProps need to be performant.
There are two things you can do to help performance during these calls to mapStateToProps. The first thing is to move any unnecessary or expensive calculations for props to the containers themselves and use useMemo as necessary such as in the example below.
const mapStateToProps = (state) => ({
propOne: getPropOne(state),
propTwo: getPropTwo(state),
unnecessaryProp: fibonacciSequence(),
})
The second leads us into the third and final part: reselect.
Reselecting selectors
Reselect is a great tool for creating, organizing, and automatically optimizing selectors. It works by allowing you to easily compose selectors together, extracting common selector logic into base selectors, and then using those selectors in your primary selectors.
export const exampleSelector = createSelector(
otherSelector,
anotherSelector,
(other, another) =>
other
.map(({ type: otherType }) =>
another.find(
({ type: anotherType}) => anotherType === otherType
)
)
.filter((value) => value !== undefined)
);
With selectors, you’re commonly filtering, sorting, mapping, etc. large sets of data. These are expensive operations that should often be memoized just like in useMemo. Reselect already handles this: every selector you make with the create selector is memoized with a shallow equality comparison of the passed state or result of prior selectors. This can give an application a decent speed boost without doing anything else if not already memoizing selectors.
Check out our React developers page, and if you’re interested, explore our career opportunities.