I have a react search input that fires during onChange and stores the new value in a session value, which is passed into a subscription. The collection has 3M+ records and this is going kind of slow… it is basically re-firing the subscription on every keystroke.
I’m guessing the easiest thing to do is throttle the input a little?
Then another improvement may be somehow cancelling the subscription call if the user is still typing?
Thanks! I actually don’t use either at this point but used lodash in the past (hipster told me it isn’t cool to use underscore).
I’ll see if I can get this goin. My function is literally this basic right now:
handleTextSearch(e){
Session.set("searchValue", {title: e.target.value}); // stores text value onChange
Session.set('isLoading', true); // shows spinner, then reset to false once subscription is done re-running
}
Remember that Meteor uses underscore so the _ object should be available in your Meteor code. I have a feeling it’s a fairly old version they’re running though. Underscore is a good option but you may need to a _.noConflict() statement immediately after requiring/importing it to avoid conflicting with Underscore.
EDIT: You beat me to it, for clarity the issue with your original code was that you were redefining the debounced function every time it was called, so it never actually debounced. Always make sure you define the debounced function outside of the function that’s calling it.
Awesome, I wouldn’t have even thought of using debounce for this. I’ve been manually creating a timer via setTimeout to wait after a keystroke before searching.