Need help understanding Node.js CPU consumption

76 views
Skip to first unread message

Sunil Agrawal

unread,
Jul 7, 2015, 8:54:29 AM7/7/15
to nod...@googlegroups.com
If my entire Node.js program is the following 

setInterval(function() {}, Infinity) 

'top' shows it consumes 15-20% CPU

If I change it to

setInterval(function() {}, 100000)

it drops down to < 1% CPU consumption.

Any insights what's going on?

TIA,
Sunil 

Ben Noordhuis

unread,
Jul 7, 2015, 10:59:43 AM7/7/15
to nod...@googlegroups.com
setInterval() in node.js follows what browsers do: intervals >
2,147,483,647 milliseconds are treated as $smallest_timeout intervals
(= 1 ms in node.js.)

In other words, `setInterval(function() {}, Infinity)` is identical to
`setInterval(function() {}, 1)`.

mscdex

unread,
Jul 7, 2015, 11:04:27 AM7/7/15
to nod...@googlegroups.com
On Tuesday, July 7, 2015 at 8:54:29 AM UTC-4, Sunil Agrawal wrote:
If my entire Node.js program is the following 

setInterval(function() {}, Infinity) 

'top' shows it consumes 15-20% CPU


What is happening here is that since `Infinity` is larger than the maximum allowed timeout value (2147483647 -- 2^31-1), so it changes the value to 1[1], causing the function to execute on *every* tick of the event loop.

Using a value of 100000 is still smaller than the maximum allowed timeout value, so in that case the function only executes every 100000 milliseconds.

[1] https://github.com/joyent/node/blob/d38e865fce93c00cb128034202fb1e26987efdbc/lib/timers.js#L208-L210

Alex Giannakakos

unread,
Jul 8, 2015, 10:02:51 AM7/8/15
to nod...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages