reselect icon indicating copy to clipboard operation
reselect copied to clipboard

Processing large/expensive data sets

Open avanderhoorn opened this issue 10 years ago • 2 comments

I'm in the situation where i need to process some large/expensive data sets. This data comes in as a batch to start and then updates stream in post the initial batch. I can handle the streaming results just fine but its the initial batch that is causing me issues. I'm finding that to process this data for large sets is taking between 30-100ms... obviously this isn't ideal and I'd much prefer to be in the 5-10ms range. Putting aside whether or not the logic can be simplified or not, I'm wondering if anyone has any thoughts on how the data might be able to be "batched" or broken up with reselect to allow incremental paints, etc to occur so I'm not causing any janking... Its possible that I could chunk the data downstream of reselect but I was wondering if anyone has run into similar problems with long running selectors and how they dealt with it.

avanderhoorn avatar Nov 25 '16 03:11 avanderhoorn

Two things come to mind:

  1. If the batch processing consists of filtering many individual items, you can memoize the filter function so that repeated calls to test each item are nearly free. I.e., your selector function is doing data.filter(filterFn), but then your filterFn which tests each item can be shortcut if that item was processed in the previous run. I've used memoizee for this. Set the memoizee cache size to something bigger than your filtered data set size.

  2. Add the requestAnimationFrame scheduler middleware (see here: http://redux.js.org/docs/advanced/Middleware.html).

Then do your dataset updates (big and small) with meta: {raf: true}. Modify the loop() function of the raf middleware to inspect and modify the queue each time it gets called...

If the queuedActions[0] is a big batch item, you can split it into multiple queue items so they each get processed (and selected, and flushed to the DOM) in their own raf frame. This combined with the cache above should give decent performance.

And going the opposite direction, if many raf items have built up in a queue (handling a stream while the browser was off-screen for example), you can (actually, need to) inspect your queue and look for opportunities to either merge items into one update, or simply ignore ones that will get replaced later in the queue anyway. My action creator functions mark my actions with additional boolean fields in the meta object to indicate whether they are allowed to be collapsed or replaced in this way so the rafScheduler code doesn't have to understand what's going on in the actions.

So all that has nothing much to do with reselect, but it will make the DOM updates that result from your selectors much more manageable.

MalcolmDwyer avatar Jan 24 '17 16:01 MalcolmDwyer

Closing due to inactivity

ellbee avatar Mar 01 '17 17:03 ellbee