Skip to content

Instantly share code, notes, and snippets.

@brycebaril
Last active Aug 29, 2015
Embed
What would you like to do?
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