Is it more efficient to use a bunch of setTimeouts or a setInterval for refreshing a bunch of objects

StackOverflow https://stackoverflow.com/questions/11377083

Question

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 setTimeout for each one.

  • Use a setInterval that runs a function every second. The functions then cycles through each Watcher checking 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.

Was it helpful?

Solution

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 the setTimeout() 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 new setTimeout() for that interval.

Here's some pseudo-code:

function orderedQueue() {
    this.list = [];
}

orderedQueue.prototype = {
    add: function(time, callback) {
        var item = {}, added = false;
        item.time = time;
        item.cb = callback;
        for (var i = this.list.length - 1; i >= 0; i--) {
            if (time > this.list[i].time) {
                // insert after the i item
                this.list.splice(i + 1, 0, item);
                added = true;
                break;
            }
        }
        // if no item was after this item, 
        // then put this on the front of the array
        if (!added) {
            this.list.unshift(item);
        }
    },
    addDelta(delta, callback) {
        var now = new Date().getTime();
        this.add(now + delta, callback);
    },
    waitNext: function() {
        // assumes this.list is properly sorted by time
        var now = new Date().getTime();
        var self = this;
        if (this.list.length > 0) {
            // set a timer for the first item in the list
            setTimeout(function() {
                self.process();
            }, this.list[0].time - now);
        }
    },
    process: function() {
        var now,item;
        // call all callbacks who's time has been reached
        while (this.list.length) {
            now = new Date().getTime();
            if (this.list[0].time <= now) {
                // remove front item from the list
                item = this.list.shift();
                // call the callback and pass it the queue
                item.cb(this);
            } else {
                break;
            }
        }
        // schedule the next item
        this.waitNext();
    }
}

And, here's generally how you would use it:

var q = new orderedQueue();
// put initial events in the queue
q.addDelta(100, f1);
q.addDelta(1000, f2);
q.addDelta(5000, f3);
q.addDelta(10000, f4);
q.addDelta(200, f5);
q.addDelta(100, f1);
q.addDelta(500, f1);
// start processing of queue events
q.waitNext();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top