Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8198425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:56:55+00:00 2026-06-07T05:56:55+00:00

Let’s say there is a set of Watcher s that need to be refreshed

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:56:56+00:00Added an answer on June 7, 2026 at 5:56 am

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an abstract parent class called shape, and that there are
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say for a moment that I have the following module in python: class
Let's say I have some text as follows: do this, do that, then this,
Let's say you have a class library project that has any number of supplemental
Let's say that each Product has a category. I want to ask the Users
Let say I have my view that I use it as a toggle button.
Let's say, I have an application that access(read/write) the file system(files inside application), Active
Let's say that I have a html form (actually I have an editor -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.