Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow running setInterval more than once per millisecond in nodejs

I have a node script which is supposed to utilize all the CPU resources a single node process could get. But I found setInterval to be too slow.

And sure enough I found this in the documentation:

When delay is larger than 2147483647 or less than 1, the delay will be set to 1.

source: https://nodejs.org/api/timers.html#timers_setinterval_callback_delay_args

Now I wonder if there is a way to reduce the limit further or if there is an alternative function that I could use.

I can't just use a normal loop because there are other asynchronous things that need to run at the same time.

Edit:
Again: I can't just use a normal loop because there are other asynchronous things that need to run at the same time. I'm not sure why this is so hard to understand.

While a normal loop is running, you are blocking the execution of everything else. It doesn't matter if you put the loop in another asynchronously executed function.

What does this mean?

Let's look at some examples:

setInterval(()=>{console.log('a')},1000) // asynchronous thing that needs to run in the background

while (true) {
    // do whatever
}

What will this code do? It will block everything. console.log('a') will not be executed continuously.

setInterval(()=>{console.log('a')},1000) // asynchronous thing that needs to run in the background
setTimeout(()=>{
    while (true) {
        // do whatever
    }
}, 1)

This will also block the execution of the intervals as soon as the while loop starts.

like image 555
Forivin Avatar asked Jan 22 '18 11:01

Forivin


People also ask

How many times does setInterval run?

setInterval will run the function sendMessage every second (1000 ms).

Does setInterval repeat?

setInterval() The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.

Can we use two setInterval in JavaScript?

Good question, but in JS you can't. To have multiple functions in the same program execute at the same time you need multi-threading and some deep timing and thread handling skills. JS is single threaded.

Can I use setInterval in node JS?

What is the use of setInterval() method in Node. js ? The setInterval() method helps us to repeatedly execute a function after a fixed delay. It returns a unique interval ID which can later be used by the clearInterval() method which stops further repeated execution of the function.


1 Answers

I believe the question belongs to node rather than to browser. You can use some of the following options (recursively/in loop) for reducing your delay time.

setImmediate

setImmediate - Schedules the "immediate" execution of the callback after I/O events' callbacks. Returns an Immediate for use with clearImmediate().

When multiple calls to setImmediate() are made, the callback functions are queued for execution in the order in which they are created. The entire callback queue is processed every event loop iteration. If an immediate timer is queued from inside an executing callback, that timer will not be triggered until the next event loop iteration.

It's from node guides:

setImmediate and setTimeout are similar, but behave in different ways depending on when they are called.

  • setImmediate() is designed to execute a script once the current poll phase completes.
  • setTimeout() schedules a script to be run after a minimum threshold in ms has elapsed.

process.nextTick

The process.nextTick() method adds the callback to the "next tick queue". Once the current turn of the event loop turn runs to completion, all callbacks currently in the next tick queue will be called.

From node guide

We recommend developers use setImmediate() in all cases because it's easier to reason about (and it leads to code that's compatible with a wider variety of environments, like browser JS.)

like image 75
The Reason Avatar answered Oct 19 '22 23:10

The Reason