Skip to content

Instantly share code, notes, and snippets.

@tristanls
Created December 21, 2011 21:11
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/1507721 to your computer and use it in GitHub Desktop.
Save tristanls/1507721 to your computer and use it in GitHub Desktop.
loop v. nextTick benchmark
var startFast;
var startSlow;
var size = 1000000; // million
var fast = function () {
for ( var i = 0; i < size; i++ ) {
( function () {
var j = i;
return function () {
//console.log( 'loop ' + j );
if ( j == ( size - 1 ) ) {
console.log( 'loop result: ' + ( new Date() - startFast ) + 'ms' );
}
};
})()();
}
};
var slow = function () {
for ( var k = 0; k < size; k++ ) {
process.nextTick( ( function () {
var j = k;
return function () {
//console.log( 'next ' + j );
if ( j == ( size - 1 ) ) {
console.log( 'nextTick result: ' + ( new Date() - startSlow ) + 'ms' );
}
};
})() );
}
};
startFast = new Date();
fast();
startSlow = new Date();
slow();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment