React Rendering #2 — Optimization
If you don’t have time to read but want to know what’s there in this post. Find the quick read 👇
On this Series
In this React rendering series, we will cover the following
React rendering basics
React rendering optimization
React rendering state
On this Post
In this React rendering optimization post, we will see the following
1. Optimization opportunities
Rendering is React’s way of knowing if it needs to make changes in the DOM, but there are certain cases where work and calculations performed during the render phase can be a wasted effort. After all, if a component’s render output is identical, there will be no DOM updates, thus the work wasn’t necessary.
Render output should always be based on the current combination of props and state, so it is possible to know ahead of time if a component’s render output will be the same so long as its props and state remain unchanged. This is the key observation on top of which optimizing React rendering is based, as it hinges on our code doing less work and skipping component rendering when possible.
2. Optimization techniques
React offers a handful of APIs that allow us to optimize the rendering process
shouldComponentUpdate (class components): Lifecycle method, called before rendering, returning a boolean (false to skip rendering, true to proceed as usual). Logic can vary as necessary, but the most common case is checking if the component’s props and state have changed.
React.PureComponent (class components): A base class that implements the previously described props and state change check in its shouldComponentUpdate lifecycle method.
React.memo() (any component): Higher-order component (HOC) that wraps any given component. It implements the same kind of functionality as React.PureComponent, but can also wrap function components.
All of these techniques use shallow equality for comparisons. Skipping rendering a component means skipping the default recursive behavior of rendering children, effectively skipping the whole subtree of components.
3. Reference memoization
Passing new references as
**props** to a child, the component doesn’t usually matter, as it will re-render regardless of when the parent changes. However, if you are trying to optimize a child component’s rendering by checking if its props have changed, passing new references will cause a render. This behaviour is ok if the new references are updated data, but if it’s a new reference to the same callback function passed down by the parent, it’s rather problematic.
As far as functional components are concerned, React provides the useMemo hook for memoizing values and the useCallback hook specifically for memoizing callbacks.
useMemo and useCallback can provide performance benefits but, as with any other memoization usage, it’s important to think about their necessity and the net benefit they provide in the long run.
A good rule of thumb is to consider using them for pure functional components that re-render often with the same props and/or might do heavy calculations and avoid them elsewhere.
4. Performance measurement
React Developer Tools provides a handy Profiler tab that allows you to visualize and explore the rendering process of your React applications. Under this tab, you will find a settings icon that will allow you to Highlight updates when components render, as well as Record why each component rendered while profiling
React’s development builds are significantly slower than production builds, so take all the measurements you see with a grain of salt as absolute times in development are not a valuable metric. Identifying unnecessary renders, memoization, and optimization opportunities, as well as potential bottlenecks, is where you should focus.