Skip to content

Instantly share code, notes, and snippets.

@xk
Created December 22, 2011 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xk/1511972 to your computer and use it in GitHub Desktop.
Save xk/1511972 to your computer and use it in GitHub Desktop.
nextTick.js
/*
2012-04-22 jorge@jorgechamorro.com
MacBookPro5,4 core2duo @ 2.53GHz, node v0.7.5:
loopsPerSecond: 103412616.3, nextTicksPerSecond: 671885.8, ratio: 153.9x times faster
beagleboard beaglebone @ 500Mhz:
loopsPerSecond: 6781040.2, nextTicksPerSecond: 27472.4, ratio: 246.8x times faster
*/
(function (ctr,t,kLoops,displayPeriod,i) {
function inc () { ctr++ }
function ƒ () { inc(), process.nextTick(ƒ) }
function display (ntps,ntpsStr,i,lps,lpsStr,ratioStr) {
ntps= ctr*1e3/(Date.now()-t); //nextTicks per second
ntpsStr= ", nextTicksPerSecond: "+ ntps.toFixed(1);
setTimeout(display, displayPeriod);
ctr= 0, i= kLoops, t= Date.now();
while (i--) { inc() }
lps= ctr*1e3/(Date.now()-t); //loops per second
lpsStr= "loopsPerSecond: "+ lps.toFixed(1);
ratioStr= ", ratio: "+ (lps/ntps).toFixed(1)+ "x times faster";
console.log( lpsStr+ ntpsStr+ ratioStr );
ctr= 0, t= Date.now();
}
displayPeriod= 2e3;
process.stdout.write('Calibrating loop');
kLoops= 2e6;
do {
ctr= 0, i= kLoops, t= Date.now();
while (i--) inc();
t= (Date.now()- t) || 1;
var ratio= (displayPeriod/2)/t;
kLoops= Math.floor(kLoops* ratio);
process.stdout.write('.');
} while ((ratio < 0.9) || (ratio > 1.1));
console.log('DONE. kLoops -> '+ kLoops);
setTimeout(display, displayPeriod);
ctr= 0, t= Date.now();
ƒ();
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment