react-native-big-calendar: Slows down when updating the 'events' array with many events

I’ve noticed that there is longer rendering time as the number of items in the events prop increases. I recorded a video when there’s a low and high number of events so show the difference.

From what I understand, day events are render in this part https://github.com/acro5piano/react-native-big-calendar/blob/master/src/components/CalendarBody.tsx#L158-L165

I haven’t done any performance measuring to understand where the time is spend to re-render.

Maybe it’s React that takes time to render a large list of items on the screen, so maybe there’s nothing we can do. Does anyone have any suggestions for something to try or trying another way to render the items? I’m happy to spend time on it but I’m not sure what to try so opening this issue for discussion…

Video with a high number of events (slows 2 seconds wait time to appear)

https://user-images.githubusercontent.com/59870708/144372421-9f5ed14f-c68e-48e7-ae9a-d96b6ae298b1.mp4

Video with a low number of events

https://user-images.githubusercontent.com/59870708/144372849-4862508d-0282-4aaa-8bf0-0d9983e00dd8.mp4

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 20 (11 by maintainers)

Commits related to this issue

Most upvoted comments

After experiencing a slowdown in month view with many events, as described, profiling pointed to a large proportion of time spent in sort, which appears to be due to the order of the sort and filter in the month view: https://github.com/acro5piano/react-native-big-calendar/blob/f738fc36d67533e3dbdcc9c4b1e2d0dfb5e7a5e8/src/components/CalendarBodyForMonthView.tsx#L175-L190

That is: all events are being sorted for every day rendered, not just the events that fall on that day. Re-ordering these calls so the sort only happens after filtering to the events that fall on the day resulted in a very appreciable speed up.

Additionally, converting the sort and filter functions there to use date-fns instead of dayjs and pre-calculating the duration in seconds for each event resulted in a further speed up, to the point where the monthly view is usable with a large number of events.

@arisyo13 's PR is merged and released on v4.16.1. Let me summerize what’s happended:

We have the event overlapping feature. The logic is checking every events, then find overlapping event(s) and count them. The previous logic was too naive, causing the performance issue if there are lots of events. Its time complexity was O(n^2). While O(n^2) looks okay for such a UI component, but actually there are comparison two dates (start end end) for each events, which is not silly.

His tuning resolved the time complexity by binary search to find overlapping events. It makes the logic efficient enough to be O(log n). Also, the loop now breaks once future events are found, because the events are sorted by time.

Correct me if I’m wrong.

Let me close this issue. According to his benchmark, rendering 500 events used to took 62 seconds but now it takes 430 milliseconds which is really fast. Big thank you for @arisyo13 again!

@arisyo13 Please let us know if you find a way to optimize it! 🙏🏼

i opened this pr https://github.com/acro5piano/react-native-big-calendar/pull/1124

@arisyo13 Please let us know if you find a way to optimize it! 🙏🏼

no update on this? this issue is a major one, need help

Im working on this. expect a PR soon

I also have this problem in mode “week”

I have same problem. Tested with both Android and iOS. Changing month, event click and everything slows down very much (takes 4-5s) when event array contains many event.

In my case, I have 98 entries in a single month. Here’s the snippet from code -

events: [
  {
    "event_id": "2122",
    "start": "2022-05-02 08:30:00",
    "start_timestamp": 1651501800,
    "end": "2022-05-02 10:30:00",
    "title": "Chad Allen",
    "color": "A6D06E"
  } x 98
]

<BigCalendar
  events={this.state.events}
  height={600}
  mode={'month'}
  onPressCell={ (date) => {this.showDateEvents(date)} } // Same issue even if I remove this prop
  date={ this.state.calendarDate } // Same issue even if I remove this prop
  onChangeDate={ this.setMonth } // Same issue even if I remove this prop
/>

Please can anyone confirm if it works correctly for them on Android/iOS with about 100 events?

Thank you.