Should I use event delegation in React?
Tags: react, javascript, performance
Published: 2020-05-02 02:00:48

## What is event delegation?

Event delegation is a performance optimization method in the world of Javascript. Let's say you have an unordered list `<ul>` with 1000 list items, and you want to do something each time a list item is clicked. With event delegation approach, instead of adding one event listener to each of the child items, you only add 1 event listener to the parent `<ul>`. It is a neat approach. You can read more about it [here](https://davidwalsh.name/event-delegate).

## Should I use event delegation in React?

The short answer is *"No"*. It does not give you any noticeable performance benefit. The reason is that React already does this performance optimization internally. If you don't believe me, check out the answer by Dan Abramov [here](https://github.com/facebook/react/issues/13635).

## Performance benchmark

I wanted to test this myself. So, I created a test project with 3000 buttons. When a button is clicked, we change its state to "Selected" in the parent container, and its background color changes. 

You can check out the two demos here. Continue reading for the source code.

- Without event delegation: https://test-event-delegation-off.now.sh/
- With event delegation: https://test-event-delegation-on.now.sh/

## No event delegation - 3000 event listeners

We attach an `onClick` handler to each of the buttons. 

```jsx
// selectedItems = new Set([1, 2, 3])
// ids = [0, 1, 2, ..., 2999]

{ids.map((id) => (
  <FancyButton
    key={id}
    id={id}
    label={id}
    isSelected={selectedItems.has(id)}
    onClick={() => handleClick(id)}
  />
))}
```

- See full source code [here](https://github.com/thawsitt/test-event-delegation-off/blob/master/src/App.js).

## With event delegation - 1 event listener

Here, we attach only one `onClick` handler to the parent div.

```jsx
const handleClick = (event) => {
  // Get id from button element
  const id = event.target.id;
  setSelectedItems((prevState) => new Set([...prevState, id]));
};


<div className="container" onClick={handleClick}>
  {ids.map((id) => (
    <FancyButton
      key={id}
      id={id}
      label={id}
      isSelected={selectedItems.has(id)}
    />
  ))}
</div>
```

- See full source code [here](https://github.com/thawsitt/test-event-delegation-on/blob/master/src/App.js).

# Results

![race-start](https://images.unsplash.com/photo-1457969414820-5fdd86fc0b84?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1948&q=80)

<center><i>Who will be the winner?</i></center>

---

## Test 1. First load

![off-start](https://i.imgur.com/ma85zXa.png)
---
![on-start](https://i.imgur.com/UCz72YY.png)

## Test 2. Interaction

![off-interaction](https://i.imgur.com/ZH74ZEC.png)
---
![on-interaction](https://i.imgur.com/WFLNWDI.png)

## Test 3. Heap snapshot

![off-snapshot](https://i.imgur.com/S9T9d8q.png)
---
![on-snapshot](https://i.imgur.com/PcfY82p.png)

## Test 4. Event Listeners count

![off-count](https://i.imgur.com/t3YLsqB.png)
---
![on-count](https://i.imgur.com/dQHoXr7.png)

# Conclusion

We didn't find any noticeable performance difference between the two. React is already optimized in most cases, so you can focus on the awesome features you are shipping. 🚀🚀🚀

Feel free to benchmark the two demos yourself and let me know if you find any different results.


- Without event delegation: 🌎 [Demo](https://test-event-delegation-off.now.sh), 🌱 [Source Code](https://github.com/thawsitt/test-event-delegation-off/blob/master/src/App.js)
- With event delegation: 🌎 [Demo](https://test-event-delegation-on.now.sh), 🌱 [Source Code](https://github.com/thawsitt/test-event-delegation-on/blob/master/src/App.js)