react-native-calendar-events
react-native-calendar-events copied to clipboard
Performance improvement: whitelist (or blacklist) for events fields
Currently, depending on the device you run on, and more importantly depending on the number of events you are going to get, a fetchAllEvents call can be really expensive and slow.
I made some tests and by removing most field and keeping title, startDate, endDate, you can win a lot.
So my proposal, inspired by #251, is to have a whitelist (or a blacklist) of field you want.
I would go for a whitelist instead of a blacklist to ensure performance cost is something user opt-in: if you want "all the things" (all fields) you have to be explicit about it, and documentation can warn you about the cost it can have. It will probably avoid issues saying "it's slooowww" and more importantly, I think most user of this lib don't need every fields. In my case, I am building a kind time tracker, I don't need (at least for now) anything except title, duration (so start,end) and calendar id. By reducing things to what is absolutely necessary, I got a huge performance boost.
So I think what we could do is to go all in: serialised events by default returns id and whitelisted fields. If user don't provide whitelist, lib will throw an error (and types/bindings can enforce that for typescript/rescript & friends).
This would be a minor breaking change to the lib that we could ship as a 3.0.0 that would only add a mandatory whitelist option to fetchAllEvents and findById.
@wmcmahan any thoughts?
I think this is a good idea and offers a simple way to speed things up. I'm of the mentality that fetching all events over a wide time range is not a great use, but rather smaller precise queries is more ideal. But I also totally understand that there are use-cases for requesting a ton of events at once, and allowing the user to opt in to specific fields would really help.
Is the serialization of the events the performance blocker? If so it should be fairly trivial to allow another optional list of fields without causing any breaking changes. For example, something like:
// This will return all fields, as default
RNCalendarEvents.fetchAllEvents(startDate, endDate, calendars);
//This will return only desired fields
RNCalendarEvents.fetchAllEvents(startDate, endDate, calendars, fields = ['id', 'title']);
[{
id: 'FE6B128F-C0D8-4FB8-8FC6-D1D6BA015CDE'
title: 'Dr. Visit'
}]