nanha
1/27/2012 - 7:55 AM

b2.js

var startFast;
var startSlow;

var size = 1000000; // million

var loopCount = 0;
var setTimeoutTest = function () {
  loopCount++;
  if ( loopCount == ( size - 1 ) ) {
    console.log( 'setTimeout result: ' + ( new Date() - startFast ) + 'ms' );

  startSlow = new Date();
  slow();
  }
};

var fast = function () {
  for ( var i = 0; i < size; i++ ) {
    setTimeout(setTimeoutTest, 0);
  }
};

var nextTickCount = 0;
var nextTickTest = function () {
  nextTickCount++;
  if ( nextTickCount == ( size - 1 ) ) 
    console.log( 'nextTick result: ' + ( new Date() - startSlow ) + 'ms' );
}

var slow = function () {
  for ( var k = 0; k < size; k++ ) {
    process.nextTick( nextTickTest );
  }
};

startFast = new Date();
fast();