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

  • Home
  • SEARCH
  • 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 8915323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:57:02+00:00 2026-06-15T04:57:02+00:00

I was wondering if anybody knows how setTimeout is implemented in node.js. I believe

  • 0

I was wondering if anybody knows how setTimeout is implemented in node.js. I believe I have read somewhere that this is not part of V8. I quickly tried to find the implementation, but could not find it in the source(BIG).I for example found this timers.js file, which then for example links to timer_wrap.cc. But these file do not completely answer all of my questions.

  • Does V8 have setTimeout implementation? I guess also from the source the answer is no.
  • How is setTimeout implemented? javascript or native or combination of both? From timers.js I assume something along the line of both:

    var Timer = process.binding('timer_wrap').Timer;`
    
  • When adding multiple timers(setTimeout) how does node.js know which to execute first? Does it add all the timers to a collection(sorted)? If it is sorted then finding the timeout which needs to be executed is O(1) and O(log n) for insertion? But then again in timers.js I see them use a linkedlist?

  • But then again adding a lot of timers is not a problem at all?
  • When executing this script:

    var x = new Array(1000),
        len = x.length;
    
    /**
     * Returns a random integer between min and max
     * Using Math.round() will give you a non-uniform distribution!
     */
    function getRandomInt (min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    
    var y = 0;
    
    for (var i = 0; i < len; i++) {
        var randomTimeout = getRandomInt(1000, 10000);
    
        console.log(i + ', ' + randomTimeout + ', ' + ++y);
        setTimeout(function () {
            console.log(arguments);
        }, randomTimeout, randomTimeout, y);
    }
    

    you get a little bit of CPU usage but not that much?

  • I am wondering if I implement all these callbacks one by one in a sorted list if I will get better performance?
  • 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-15T04:57:03+00:00Added an answer on June 15, 2026 at 4:57 am

    You’ve done most of the work already. V8 doesn’t provides an implementation for setTimeout because it’s not part of ECMAScript. The function you use is implemented in timers.js, which creates an instance of a Timeout object which is a wrapper around a C class.

    There is a comment in the source describing how they are managing the timers.

    // Because often many sockets will have the same idle timeout we will not
    // use one timeout watcher per item. It is too much overhead.  Instead
    // we'll use a single watcher for all sockets with the same timeout value
    // and a linked list. This technique is described in the libev manual:
    // http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Be_smart_about_timeouts
    

    Which indicates it’s using a double linked list which is #4 in the linked article.

    If there is not one request, but many thousands (millions…), all
    employing some kind of timeout with the same timeout value, then one
    can do even better:

    When starting the timeout, calculate the timeout value and put the
    timeout at the end of the list.

    Then use an ev_timer to fire when the timeout at the beginning of the
    list is expected to fire (for example, using the technique #3).

    When there is some activity, remove the timer from the list,
    recalculate the timeout, append it to the end of the list again, and
    make sure to update the ev_timer if it was taken from the beginning of
    the list.

    This way, one can manage an unlimited number of timeouts in O(1) time
    for starting, stopping and updating the timers, at the expense of a
    major complication, and having to use a constant timeout. The constant
    timeout ensures that the list stays sorted.

    Node.js is designed around async operations and setTimeout is an important part of that. I wouldn’t try to get tricky, just use what they provide. Trust that it’s fast enough until you’ve proven that in your specific case it’s a bottleneck. Don’t get stuck on premature optimization.

    UPDATE

    What happens is you’ve got essentially a dictionary of timeouts at the top level, so all 100ms timeouts are grouped together. Whenever a new timeout is added, or the oldest timeout triggers, it is appended to the list. This means that the oldest timeout, the one which will trigger the soonest, is at the beginning of the list. There is a single timer for this list, and it’s set based on the time until the first item in the list is set to expire.

    If you call setTimeout 1000 times each with the same timeout value, they will be appended to the list in the order you called setTimeout and no sorting is necessary. It’s a very efficient setup.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

wondering if anybody knows the reason that on android 2.3.5 the -webkit-animation-fill-mode:forwards; does not
The title pretty much covers it. Wondering if anybody knows this. I'm seeing both
More curious than anything, but I'm wondering if anybody knows why this the scrollviewer
I'm wondering if anybody knows of a code snippet anywhere that allows you to
i was wondering if anybody knows of a nice scripting language that can be
I was wondering if anybody knows the total length that a post global could
I'm wondering if anybody knows how to get multiple counts from a single query
I was wondering if anybody knows of a way to retrieve the actual T-SQL
I've downloaded the dll's for ImageMagick and am wondering if anybody knows of some
I was wondering if anybody knows how I would go about detecting when the

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.