I think that if we swapped the names of nextTick and setImmediate, most of the confusion would go away.
It seems pretty clear to me that both functions are useful: nextTick allows you to ensure that a function is "always asynchronous" without incurring a penalty in terms of overhead, and setImmediate allows you to distribute computation over many turns of the event loop while ensuring that I/O doesn't get starved. If setImmediate worked through the entire queue of callbacks on the next turn of the event loop, that reintroduces the possibility of starvation. The current behavior is more expensive, but (IMO) more useful as a counterpart to nextTick. I also think it's more intuitive, but that's a matter of opinion. What this suggests is that the docs need to be clearer in terms of explaining when you'd want to use one instead of the other (and also maybe gently guiding people away from using setTimeout(fn, 0), although maybe we could modify setTimeout(fn, 0) to process the entire callback queue on a further turn of the event loop, because setTimeout(0)'s semantics are sufficiently vague and magic is great!). On Fri, Jul 5, 2013 at 8:46 AM, Timothy J Fontaine <[email protected]>wrote: > I'll include here my reply from the issue > > That is certainly how it was designed, to avoid starving the loop we > decided to only run one callback per tick. > > Prior to 0.11 nextTick checks to see how "deep" you are and warns that you > may essentially be starving the loop, in 0.11 that check is no longer there > and nextTick will soldier on and do what you told it to do and fire every > cb you scheduled. > > The differentiating factor for setImmediate is that we're *always* > yielding to the loop to let it do IO. Yes, that means that cb queued with > setImmediate will fire more slowly than nextTick. > > > On Fri, Jul 5, 2013 at 7:03 AM, Ben Noordhuis <[email protected]> wrote: > >> On Fri, Jul 5, 2013 at 3:11 PM, Gagle <[email protected]> wrote: >> > I was writting a benchmark utility when I found the isaacs node-bench >> module >> > and found this code: >> > >> >> var nt_ = typeof setImmediate === 'function' ? setImmediate : >> >> process.nextTick >> >> var ntN = 0 >> >> function nt(fn) { >> >> if (ntN++ < 100) >> >> return process.nextTick(fn) >> >> nt_(fn) >> >> ntN = 0 >> >> } >> > >> > >> > If you rewrite in a more cleaner way... >> > >> > >> >> var async = (function (){ >> >> var i = 0; >> >> return function (fn){ >> >> if (i++ < 100){ >> >> process.nextTick (fn); >> >> }else{ >> >> setImmediate (fn); >> >> i = 0; >> >> } >> >> } >> >> })(); >> >> >> >> async (function (){ >> >> //Asynchronous code >> >> }); >> > >> > >> > This code permits to use nextTick without reaching the call stack error. >> > >> > While I was writting my benchmark module I noticed 2 things: >> > >> > - Recursively calls to setImmediate produces inconsistent values: 100, >> then >> > 150, then 70, etc. when I should get 100, 105, 101, 95, etc. With >> nextTick >> > (using the above function) the results are pretty consistent. Why? >> > - setImmediate is much more slower than nextTick, aproximately 10 times >> > slower. Why? >> >> It's because setImmediate() does what process.nextTick() says it does >> but actually doesn't: schedule the callback at the end of the current >> tick / start of the next tick. >> >> That said, I believe the current setImmediate() implementation has a >> bug where it spreads out multiple callbacks over several ticks, i.e. >> one callback per tick. Tracking issue is >> https://github.com/joyent/node/issues/5798 >> >> -- >> -- >> Job Board: http://jobs.nodejs.org/ >> Posting guidelines: >> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines >> You received this message because you are subscribed to the Google >> Groups "nodejs" group. >> To post to this group, send email to [email protected] >> To unsubscribe from this group, send email to >> [email protected] >> For more options, visit this group at >> http://groups.google.com/group/nodejs?hl=en?hl=en >> >> --- >> You received this message because you are subscribed to the Google Groups >> "nodejs" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > -- > -- > Job Board: http://jobs.nodejs.org/ > Posting guidelines: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > You received this message because you are subscribed to the Google > Groups "nodejs" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > > --- > You received this message because you are subscribed to the Google Groups > "nodejs" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
