Created
December 21, 2011 23:32
-
-
Save tristanls/1508208 to your computer and use it in GitHub Desktop.
loop v. nextTick benchmark ( no closures )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var startFast; | |
var startSlow; | |
var size = 1000000; // million | |
var loopCount = 0; | |
var loopTest = function () { | |
loopCount++; | |
if ( loopCount == ( size - 1 ) ) | |
console.log( 'loop result: ' + ( new Date() - startFast ) + 'ms' ); | |
}; | |
var fast = function () { | |
for ( var i = 0; i < size; i++ ) { | |
loopTest(); | |
} | |
}; | |
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(); | |
startSlow = new Date(); | |
slow(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment