Skip to content

Instantly share code, notes, and snippets.

@brycebaril
Last active August 29, 2015 13:58
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 brycebaril/10311448 to your computer and use it in GitHub Desktop.
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
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