Closed
Description
Affected Versions: 0.11+
The issue came up during testing of node-toobusy. I found that in newer Node versions, there seemed to be an artificial delay introduced between setTimeout() calls if there is a significant amount of synchronous work in them. The delay seems to always equal the amount of work done, as if Node is measuring the time taken by the function and delaying it by that amount of time to keep the loop free.
Reproduction script:
function tightWork(duration) {
var start = Date.now();
while ((Date.now() - start) < duration) {
for (var i = 0; i < 1e5;) i++;
}
}
var count = 0;
var last = Date.now();
function load() {
if (count++ > 10) return;
// This measures the amount of time between setTimeout() being called,
// and it actually firing.
console.log('tick delta:', (Date.now() - last));
tightWork(100);
last = Date.now();
setTimeout(load, 0);
}
load();
Output (similar on 0.11.x and above):
$ node -v
v5.6.0
$ node index
tick delta: 13
tick delta: 1
tick delta: 105
tick delta: 104
tick delta: 105
tick delta: 105
tick delta: 105
tick delta: 104
tick delta: 105
tick delta: 105
tick delta: 105
Yet this doesn't happen on 0.10:
$ node -v
v0.10.41
$ node index
tick delta: 8
tick delta: 2
tick delta: 1
tick delta: 1
tick delta: 1
tick delta: 1
tick delta: 1
tick delta: 1
tick delta: 1
tick delta: 1
tick delta: 1