4

I have a java script file which runs using nodeJS. I am using the setTimeout() method to create a sort of timer which will run some code after a user specified time. As multiple users will be using this code will there be a problem with running lets say 1000 setTimeout methods in parallel? Is there a maximum number that can run simultaneously or will it just make the program run slower the more I have?

Ortovox
  • 382
  • 10
  • 18
  • 1
    I'm not aware of any hard limits, I imagine it's more a factor of the amount of memory you have available. – c1moore May 23 '20 at 16:56
  • 1
    Important note: the setTimeout calls do not run “in parallel”. The callbacks are placed on an ordered queue (first timeout first) and will be run sequentially in order as the trigger time occurs. The biggest “cost” is then the memory required to keep the scheduled callback functions *and* all reachable objects from the callback closures alive. – user2864740 May 23 '20 at 17:09
  • In other comments you've asked for more specific coding recommendations. To have an ability to do that, we need to see your existing code and understand exactly what you're trying to do. Specific recommendations need to see the specific problem you are working on and the code you have for it so far. You will find here that you will pretty much always get more accurate and more specific answers if you show us your actual code. Or put more succinctly, "questions about code here should include that code". – jfriend00 May 23 '20 at 17:10
  • 2
    FYI, timers are a very efficient resource in node.js. You can easily have millions. They are kept in an ordered and bucketed list where all timers set to fire at the same time are in the same bucket. All node.js does in the event loop is see if the time has arrived for the timer at the start of the list. If so, it fires it and checks the next one on the list. If not, nothing to do yet. Creating a new timer just has to do a search to figure out where to insert it in the linked list. So, the most costly operation is inserting a new timer in the list. – jfriend00 May 23 '20 at 17:11

2 Answers2

6

There is no hard limit to how many setTimeouts you can have at once, it'll just use more memory the more you have, but if you have a lot and exact timing isn't important you may just want to load the events into an array and have a single setInterval going through the array and running them if the timeout finished.

Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • Okay thank you so much! However could you elaborate on how I would implement your proposed solution? I am pretty new to JS so am not quite sure how you would have a single ```setInterval``` loop through an array? – Ortovox May 23 '20 at 17:07
  • You have an array and whenever you want a timeout you push an object to that array with a time and a function (if you want one second in the future then the time might be something like `Date.now() + 1000`), then you have an interval that runs every once in a while that goes through the array, checks if `Date.now()` is greater than the time, and if it is, it runs the function and removes the object from the array. – Aplet123 May 23 '20 at 17:10
  • Thank you guys so much! This was my first ever post so I didn't know what to expect but your advice has been brilliant and I now know what to do so thank you all again! – Ortovox May 23 '20 at 17:15
0

--If there is a block of code that should execute multiple times, setInterval() can be used to execute that code. setInterval() takes a function argument that will run an infinite number of times with a given millisecond delay as the second argument.

I believe that for every user they have their own set timeout function and when they get over then their request will be fulfilled. One whose set timeout ends first will get the high priority.

VR7
  • 112
  • 3
  • 16