Skip to content

Instantly share code, notes, and snippets.

@tristanls
Created December 22, 2011 16:07
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 tristanls/1510817 to your computer and use it in GitHub Desktop.
Save tristanls/1510817 to your computer and use it in GitHub Desktop.
loop v nextTick benchmark ( no closures )
var startLoop;
var startNextTick;
var size = 1000000; // million
var loopCount = 0;
var noop = 0;
var loopTest = function () {
loopCount++;
if ( loopCount < ( size - 1 ) )
noop++;
if ( loopCount == ( size - 1 ) )
console.log( 'loop result: ' + ( new Date() - startLoop ) + 'ms' );
};
var loop = function () {
for ( var i = 0; i < size; i++ ) {
loopTest();
}
};
var nextTickCount = 0;
var nextTickTest = function () {
nextTickCount++;
if ( nextTickCount < ( size - 1 ) )
process.nextTick( nextTickTest );
if ( nextTickCount == ( size - 1 ) )
console.log( 'nextTick result: ' + ( new Date() - startNextTick ) + 'ms' );
}
var nextTick = function () {
nextTickTest();
};
startLoop = new Date();
loop();
startNextTick = new Date();
nextTick();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment