I have been having some strange issues with node.js using windows machines which does not occur on linux machines.
When using the setInterval() function at low delay settings (< 100ms): Essentially, the provided function is called less often than I would expect, in a way that is very consistent. When using the example code I quickly wrote up below, windows machines seem to give consistently different results than linux machines, even when running on the same hardware.
//should be time in MS between executions
var delay = 10;
//how many seconds to run the test
var testPeriods = 10;
var counter = 0;
var startTime = new Date().getTime();
var interval = setInterval(() => {
++counter;
if (new Date().getTime() >= startTime+1000*testPeriods) {
console.log('Mean Function Calls per Second:', counter/testPeriods);
clearInterval(interval);
}
}, delay);
Some test data, from my pc which dual-boots linux and windows:
Delay (ms) | Expected | Linux result | Windows result
-----------+----------+--------------+---------------
100 | 10 | 10.0 | 9.2
50 | 20 | 20.0 | 16.0
25 | 40 | 39.8 | 32.0
10 | 100 | 98.4 | 63.8
You'll notice that the linux results match almost perfectly until the lower delay settings, and even they it isn't that far off. The windows results, on the other hand, fall well short.
I figure it's likely that the windows version of node is poorly optimized compared to the linux version. So at first, I assumed that the function I was providing simply took too long to execute, thereby delaying the next execution. However, this does not seem to be the case. After all, if I assume the provided function takes similar time to execute no matter what, then I know the windows machine can execute it up to ~63 times a second at the lowest delay setting. So why then does it only execute ~32 times when it should be doing it ~40 times (with delay @ 25ms)?
If anybody could give me some insight into why this is happening or what I am doing wrong, that would be much appreciated.
EDIT: Simplified code at the suggestion of @jfriend00, and updated test results to match.