Let’s say there is a set of Watchers that need to be refreshed periodically. They each may have a different refresh interval. There may be several hundred such Watcher items at any given moment. The refresh time for any Watcher can range from a second to several minutes or hours.
Which is better?
-
Use a separate
setTimeoutfor each one. -
Use a
setIntervalthat runs a function every second. The functions then cycles through eachWatcherchecking to see if it needs to be refreshed.
At first I assumed that the native code implementation of setTimeout would be more efficient than a JS function that does checking, but it’s really a question of how setTimeout is implemented, how much overhead each timeout takes on a per-tick basis, and how well the number of timeouts scales.
I’m asking this for a Node application so the specific engine I’m referring to is V8, but it’d be cool if anyone knows the details for other engines as well.
Here’s one idea that should be pretty efficient regardless of how setTimeout or setInterval is implemented. If you have N events scheduled for N different times in the future, create an array of objects where each object has a property for the time that the event is due and a property that tells you what type of event it is (a callback or some other identifier). Initially sort that array by the time property so the next time is at the front of the event and the furthest time is at the end.
Then, look at the front of the array, calc the time until that event and do
setTimeout()for that duration. When thesetTimeout()fires, look at the start of the array and process all events who’s time has been reached. If, after processing an event, you need to schedule it’s next occurrence, calc the time in the future when it should fire and walk the array from start to finish until you find an event that is after it and insert this one right before that event (to keep the array in sorted order). If none is found, insert it at the end. After processing all events from the front of the array who’s time has been reached, calc the delta time to the event at the front of the array and issue a newsetTimeout()for that interval.Here’s some pseudo-code:
And, here’s generally how you would use it: