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.
