When is derived data too complex for redux selectors?

Viewed 312

Common advise is to keep the state minimal and use memoized selectors (like the reselect lib) for derived data. I understand that there are also cases when it is appropriate to put it in the store. I have lots of derived data in my app, which is somewhat expensive to generate, but due to the nature of how it's used, it's not necessary to generate it often. At what point is the data too complex to be appropriate for a selector and should be put into the store?


More details:

I have time series data on which I run some calculations to generate derived "views" of the data. The records in each view are calculated based on all the records that came before.

To provide a more concrete, somewhat contrived example:

Currently I am doing this all in indexedDB without redux.

I have one main table which is purely user input:

type Earnings = {
    id: number;
    time: Date;
    amount: number;
    category: 'A' | 'B' | 'C' | 'D';
};

And I have a bunch of hooks that generate "view" tables when the Earnings data changes.

type EarningsByDay = {
    id: number;
    date: Date; // only used to date part
    amountTotal: number;
    maxToDate: number;
};

type EarningsByDateAndCategory = {
    id: number;
    date: Date; // only used to date part
    category: 'A' | 'B' | 'C' | 'D';
    amountTotal: number;
    maxToDate: number;
};

In the views above, the maxToDate for EarningsByDay is computed by finding the max amountTotal from all EarningsByDay records whose date is less than the date of the current item. Likewise for EarningsByDateAndCategory.

If the user edits/deletes/inserts earnings in the past then all the derived data that came after needs to be regenerated. However, this is rare, and most of the time the user will be adding Earnings records chronologically (which means all I have to do is search the past records to find the max).

As part of my plan to move to redux I was planning to only store the Earnings records in indexedDB and load them all on launch into the store (about 5,000 records for one year of data). Then I would use selectors for the derived "views".

My concern is that this is too heavy to be appropriate for selectors. Also, the way reselect memoizes, I would end up regenerating the all the derived data when appending record.

1 Answers
Related