this.resize = _.bind(_.debounce(this.resize, 200), this);
Read below to learn how several Underscore.js Function utilities can be used to improve performance and responsiveness of your apps.
Lets start with a real example where we used _.debounce() to improve performance for end users of Sugar 7.5.
What debounce does is postpone execution of a function until after an elapsed time has passed since a function was last called. In this case, the throttledSearch function gets called every time there is a key press in a Sugar 7 Quick Search input box. Without debounce, if a user was trying to type a search term like “Acme” then we’d be triggering search for “A”, “Ac”, “Acm”, and then “Acme”. This creates a load on the server and gives impression that the search feature isn’t working properly because of the delay in displaying the final search results due to all the previous unnecessary requests.
Using _.debounce(fn, 400) means that searches will not be triggered until the user pauses typing for almost half a second (400 milliseconds).
_.throttle() is similar except that it triggers the wrapped function at most once for every given time interval. This makes it useful to implement some sort of rate limiting if you expect many requests to occur faster than they could be reasonably handled.
There’s a lot more to Underscore than what I’ve described here. I hope you take the time to explore it and incorporate usage of it into your Sugar 7 apps and customizations!