How to structure your redux store to improve rendering performance?

This post is continuation of Tips to optimise rendering of a set of elements in React

Note: this post designed specially for React + Redux applications. Also, this tip works for react-redux v5. I was not able to apply the same technique with react-redux v4. Probably some of its inner listeners were too slow.

The very common way to store some set of elements in an application is to store items as an array:

Probably you will have a component in your application that will render the set of elements:

If you need to update an item in the list you will have to update whole list:

Every update of an item will produce update of the whole “TargetsList” view.

I created a simple demo with such approach (with testing code): http://codepen.io/lavrton/pen/xRgYbL

Updating an item in the array of size 1000 takes about ~21ms on my machine. In the previous post, I described an approach of optimizing such rendering by making child components “smarter” and adding some logic into “shouldComponentUpdate” of list component. But almost the same result can be achieved much simpler by changing state shape.

How to optimize?

If you are using https://github.com/reactjs/react-redux you can simply improve performance by changing state shape to:

Then we will need to change “TargetsList” a bit:

Please, note, that in this case, I am passing item ID into child view, not the whole item. In this case “TargetView” can not be “dumb” component. It should be connected to store:

As “TargetView” is connected to store, it will update itself when its “target” data is updated. It is important that if we update an item in a list, “TargetList” will be NOT updated, because “targetsOrder” is the same. In the same cases, it may improve performance a lot. Demo: http://codepen.io/lavrton/pen/ZBLrWp

Updating an item takes 2.2ms on my machine. It is almost 10x faster then with previous shape of the state!