FullStack Labs

Please Upgrade Your Browser.

Unfortunately, Internet Explorer is an outdated browser and we do not currently support it. To have the best browsing experience, please upgrade to Microsoft Edge, Google Chrome or Safari.
Upgrade

How and When to Optimize Components in React and Redux

Written by 
Matthew Snyder
,
Mid-Level Software Engineer
How and When to Optimize Components in React and Redux
blog post background
Google AMP vs. Other Frameworks - When to Use Each
How to Create a Simple File-Transfer WebRTC React Web Application
Using Split Flags with React to Control Feature Deployments

Table of contents

If you have ever had a function that brought your app to a stuttering halt, you may have realized that there is only so much you can do to optimize the function yourself. 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.

Matthew Snyder
Written by
Matthew Snyder
Matthew Snyder

When I first got a computer, I learned everything I could and tried making simple viruses for fun. But over time, I realized I enjoyed building things more. I love watching projects come to life and seeing the ways that abstract concepts become solutions to real problems. My favorite technology to work with is JavaScript because of its explosive growth over the past few years and the community that goes with it. In my free time I enjoy going on walks.

FullStack Labs Icon

Let's Talk!

We’d love to learn more about your project.
Engagements start at $75,000.

company name
name
email
phone
Type of project
How did you hear about us?
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.