Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of setTimeout in node?

For my webapp I'm going to need to have many timeouts running at once at any given point, possibly around 10,000-100,000. What I'm wondering is how well this function scales.

I don't need it to be that accurate, mostly accurate within 10-100 ms. Would it be better to have a single function run on an interval (say, to run every 50 ms), that checks the current datetime compared to the saved datetime and invokes the function if so?

Does anyone have any insight in to the underlying implement of setTimeout and can shed some light as to how well it can be used en-masse?

More questions I had: Does anyone know of a limit to how many timeouts can be running at once? Also, with both approaches I'm concerned about there not being enough time to process each timeout per interval and it getting "behind" in terms of triggering the timeout function in time.

like image 523
Lawrence Douglas Avatar asked Jun 26 '16 06:06

Lawrence Douglas


People also ask

Does setTimeout impact performance?

No significant effect at all, setTimeout runs in an event loop, it doesn't block or harm execution.

Does setTimeout work in node JS?

setTimeout is a built-in Node. js API function which executes a given method only after a desired time period which should be defined in milliseconds only and it returns a timeout object which can be used further in the process.

Which is faster setTimeout or Promise?

. An immediately resolved promise is processed faster than an immediate timeout.

Is setTimeout better than setInterval?

To cancel the execution, we should call clearTimeout/clearInterval with the value returned by setTimeout/setInterval . Nested setTimeout calls are a more flexible alternative to setInterval , allowing us to set the time between executions more precisely.


1 Answers

Actually you cannot determine the exact interval between timeouts, because all it does is it pushes your callback to the callback queue after the threshold and when the event loop will get that callback, push to the call stack and execute it - is non-deterministic. The same is with intervals. You may appear in a situation, when, for example, 5 callbacks will be executed one after another without a delay. This is javascript ))

like image 64
David Gabrielyan Avatar answered Oct 22 '22 00:10

David Gabrielyan