Skip to content

Instantly share code, notes, and snippets.

@xk
Created September 14, 2011 09:13
Show Gist options
  • Select an option

  • Save xk/1216160 to your computer and use it in GitHub Desktop.

Select an option

Save xk/1216160 to your computer and use it in GitHub Desktop.
test node's timers
// 2011-09-15 jorge@jorgechamorro.com
// http://groups.google.com/group/nodejs-dev/msg/cf392e131b5fcd3a
// http://groups.google.com/group/nodejs-dev/browse_thread/thread/922a30cf88a1b784
var ms= 5;
var ctr= 0;
var RUN= 1;
function f () {
if (RUN) {
ctr++;
setTimeout(f, ms);
}
}
f();
var t0= Date.now();
setTimeout(function next () {
t0= Date.now()- t0;
var is= Math.floor((ctr/t0)*1e3);
var shouldBe= Math.floor(1e3/ms);
console.log([ms+ ' ms', ctr+ ' counts', t0+ ' ms elapsed time', is+ ' Hz', 'SHOULD BE: '+ (ms ? shouldBe : 'flat-out ') + ' Hz'].join(', '));
if (ms--) {
ctr= 0;
t0= Date.now();
setTimeout(next, 2e3);
}
else {
RUN= 0;
setTimeout(f, 666);
}
}, 2e3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment