Created
April 20, 2011 21:00
-
-
Save xk/932844 to your computer and use it in GitHub Desktop.
Passing a float (instead of an integer) drives node's setTimeout nuts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sets kCount timeouts of no more than 5 ms using floats and using integers | |
// and logs the times at which they are actually triggered. | |
var t0; | |
var log= []; | |
var kCount= 20; | |
function go () { | |
var i= kCount; | |
var cb= f('FLOAT'); | |
while (i--) setTimeout(cb, Math.random()*5); | |
var i= kCount; | |
var cb= f('INT'); | |
while (i--) setTimeout(cb, Math.floor(Math.random()*5)); | |
t0= Date.now(); | |
} | |
function f (t) { | |
return function () { | |
log.push([Date.now()-t0, t]); | |
}; | |
} | |
function printLog () { | |
var txt= ''; | |
log.forEach(cat); | |
process.stdout.write(new Buffer(txt)); | |
process.stdout.flush(); | |
function cat (t, i) { | |
txt+= '[' + i + '] ' + t[0] + ' ms, '+ t[1]+ '\n'; | |
} | |
} | |
go(); | |
process.on('exit', printLog); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment