Sitemap
Cosmin Atanasiu

Cosmin Atanasiu

softwareengineer, soccer player, thinker

React Component Performance

Identifying unnecessary rendering

3 min readMar 8, 2017

--

Note: Before reading this article, please make sure you understand how to optimize React component performance using `shouldComponentUpdate`. See: Optimizing React Performance

The question I’m trying to answer is:

Will my component call the render() method even if nothing has changed visually ?

Ideally, we never want a component to render unless something actually changed visually. For this we’ll use thepure render concept from React:

Pure Render: the DOM will update if and only if the props or state changed.

So this means, that if our props haven’t changed, our components shouldn’t be redrawn in the HTML DOM, or even in React’s Virtual DOM. Basically the render function shouldn’t even be called. I’ll skip the conversation on why this is a good thing, that’s a discussion for another blog post (or the comments section).

1. Pure Functions

Starting with a Pure Function:

Pure Function Component

In this case, we expect the component not to be redrawn in the container if the carDetails haven’t changed:

Container Using the Car Pure Function Component

The Problem

What happens when chaging the props of one car ? Ideally, we only want only one of the cars to redraw:

setTimeout(() => {
firstCarDetails.name = 'Toyota'
}, 1000);

To demonstrate the problem, if you’re using React 0.13 what you end up with is:

# => rendering Container
# => rendering Car component
# => rendering Car component
# => rendering Container
# => rendering Car component
# => rendering Car component => why is this here, since props for only one car changed ?

This is a problem. The second car was rendered even though its props did not change.

Get Cosmin Atanasiu’s stories in your inbox

Join Medium for free to get updates from this writer.

After React 0.14, when using pure functions, what we have is:

# => rendering Container
# => rendering Car component
# => rendering Car component
# => rendering Container
# => rendering Car component

This is what we’d want, to avoid redrawing the second car. For now it may seem like the problem is solved, but it’s not. Let’s have a look at what happens when we use components instead of pure functions. Again, I’ll skip the conversation on why people choose to use classes instead of pure functions, but let’s just consider the cases when we want to use inheritance and use the lifecycle methods, and that’s not possible with Pure Functions.

2. Regular Components

Regular Component

Believe it or not, this component will behave exactly as the pure functions from React 0.13: components which haven’t actually changed their props will be re-rendered. This is because the Component has no idea whether or not props have changed, and doesn’t implicitly ask.

To change this, React lets us use shouldComponentUpdate and thus only render the component if the props have changed.

Regular Component with Pure Rendering

3. Pure Components

Let’s take the above example one step further: if we used Immutable.JS for props, then we could use one single comparison, to determine if props.details have changed. Without Immutable.JS this comparison wouldn’t be possible.

Regular Component with Pure Render & Immutable Props

Guess what ? React already provides this functionality out of the box for pure components

Pure Component with Immutable Props

The PureComponent provided by React by default, automatically adds a shouldComponentUpdate function which checks all your props if they’ve changed.

These are a few ways to improve performance of your React components. But when we work with large applications, what matters more is actually getting an answer to the question above, and more specifically: which components are re-rendering even though props haven’t changed. For this, you can use Perf to diagnose where your application is wasteful.

Future Reading

Above, I said even though props haven't changed as if this is something easy to determine. But comparison is non-trivial in JS and will cause components to behave AS IF props have changed, though their values haven’t. E.g.:

{test: 'test'} == {test: 'test'} // => false
(function(){}) == (function(){}) // => false

Whenever you pass in (new) objects or (new) functions as props, this becomes a real performance issue. To read more on this anti-pattern, here’s a really nice blog post on it: react-js-pure-render-performance-anti-pattern

--

--

Cosmin Atanasiu
Cosmin Atanasiu

Written by Cosmin Atanasiu

softwareengineer, soccer player, thinker