A Simple List Render Optimization For React 🏎
Tags: javascript, react, performance, webdev
Published: 2018-09-22 14:13:03

---
title: A Simple List Render Optimization For React 🏎
published: true
description: List rendering in React can be optimized with a simple change to how we use the key prop.
tags: Javascript, React, Performance, Webdev
cover_image: https://thepracticaldev.s3.amazonaws.com/i/4u2rrbh0gfqlhmi57ji7.jpg
---
Cover photo by [George Brynzan](https://unsplash.com/photos/NhepANBDBKo?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/search/photos/speed?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

Yesterday I was watching a talk by Ben Ilegbodu at React Alicante called Help! My React App is Slowwwww! in which Ben discussed some optimizations developers can make to help improve performance of React applications. He goes over many of the potential bottlenecks that may arise such as unnecessary DOM updates, reconciliation and unnecessary object creation. It’s a really interesting talk and I encourage you to watch it (link below) but what I found most interesting was his first point about unnecessary DOM updates.

{% youtube LBzJFcZsPBQ %}

When trying to optimize performance we look for actions that are slower than the majority of other actions and try to minimize the amount of times we perform these actions. It turns out updating the DOM is a very time consuming operation. It is in fact so time consuming that React has a process called reconciliation that exists to try and avoid unnecessary updates.

Unfortunately as Ben shows in his talk — and as I will show in this post — there are still situations where reconciliation will not be able to help us. However we don’t need to lose hope because there are some simple tweaks we can make to address the issue.

##The 🔑 to Lists

This is a really handy trick you can use to optimize the rendering of list items in React. Suppose you have a page that displays a list of items and is defined as follows (click the button to see the code on codesandbox):

[![Edit r4y5lj0qxn](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/r4y5lj0qxn)

When the button is clicked, it will add an item to the list. This will then trigger an update to the DOM to display our new item along with all the old items. If we look at the DOM inspector while clicking the button we see the following (orange indicates the node is updating):

{% youtube 02oJsIemKwQ %}

See how all the list items are updated? If we think about this for a moment this doesn’t actually seem like an ideal update. Why can’t we just insert the new node without having to update all the other nodes? The reason for this has to do with how we are using the map function in the List component.

{% gist https://gist.github.com/pureooze/e5a3aaeb50e3cedaa22a1442da0e18e1 %}

See how we are setting the key for each list item as the index? The problem here is that React uses the key to determine if the item has actually changed. Unfortunately since the insertion we are doing happens at the start of the list, the indexes of all items in the list are increased by one. This causes React to think there has been a change to all the nodes and so it updates them all.

To work around this we need to modify the map function to use the unique id of each item instead of the index in the array:

{%gist https://gist.github.com/pureooze/e522dbaa1f244348309423c06846a92c %}

And now when we click the button we see that the new nodes are being added without updating the old ones:

{% youtube KjWehU2HlQk %}

So what’s the lesson?

> Always use a unique key when creating lists in React (and the index is not considered unique)!

🚨 Now there is one thing to address and that is the situation where you do not have a truly unique id in your arrays. The ideal solution is to find some unique key which may be derived by combining some values in the object together. However in certain cases — like an array of strings — this cannot be possible or guaranteed, then you must rely on the index to be the key. 🚨

So there you have it, a simple trick to optimize list rendering in React! 🎉

**If you liked this post be sure to [follow this blog](https://dev.to/pureooze) and [follow me on twitter](https://twitter.com/pureooze).**

**P.S.: Looking to contribute to an open source project? Come contribute to Saka, we could use the help! You can find the project here: [https://github.com/lusakasa/saka](https://github.com/lusakasa/saka)**