Last active
August 29, 2015 13:58
-
-
Save brycebaril/10311448 to your computer and use it in GitHub Desktop.
An interesting little demo to demonstrate some of the intricacies and changes with the Node.js event loop
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 counter = 0 | |
var start = Date.now() | |
function foo(from, when) { | |
console.log(counter, when, from, process._getActiveHandles().length, Date.now() - start) | |
counter++ | |
var scheduledAt = counter | |
if (counter == 2 || counter == 25) { | |
process.nextTick(function () { | |
foo("nextTick", scheduledAt) | |
setImmediate(foo, "nextTick->setImmediate", scheduledAt) | |
}) | |
} | |
else if (counter == 6) { | |
setTimeout(function () { | |
foo("setTimeout", scheduledAt) | |
setImmediate(foo, "setTimeout->setImmediate", scheduledAt) | |
}, 0) | |
} | |
if (counter < 25) { | |
setImmediate(foo, "foo", scheduledAt) | |
} | |
} | |
foo("root", counter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment