Skip to content

Instantly share code, notes, and snippets.

@xk
Created April 20, 2011 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xk/932844 to your computer and use it in GitHub Desktop.
Save xk/932844 to your computer and use it in GitHub Desktop.
Passing a float (instead of an integer) drives node's setTimeout nuts.
// 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