Well, think of it roughly as an optimized version of this:
while(stuffLeftToDo) {
checkCurrentTickTasks();
checkIOEvents();
}
When you do process.nextTick() in your code, it'll add the supplied
function to a queue to be executed at the beginning of the next
iteration of the event loop (after any I/O events have occurred in the
current tick). When the IO events are finished, the next iteration/
tick begins (if there are still things to do). Node first checks if
there are any functions queued to be executed, and executes those in
order. It then checks for I/O events, and repeats the process.
It's in src/node.js and src/node.cc (there a JS component plus some
C++ bindings).
The reason process.nextTick() is slower than a direct loop is (mostly) twofold.
1) V8 knows impressively well how to optimize loops, closures less so.
Your benchmark creates one million closures. That's going to hurt.
2) process.nextTick() defers your callback to the next tick. In
between, everything else runs - other callbacks in your application,
Node's under-the-hood machinery. There will be a delay even if it's
only microseconds.
3) process.nextTick() itself could be optimized more. I did some work
on that a while ago but got distracted by other things.
With your particular example, #1 is the great performance killer.
Do you realize that all the nextTicks in your test are running at the same tick/event loop turn ?
--
Jorge.
You are comparing two very different things here. process.nextTick()
defers the given function, that means it will not execute it until the
Javascript interpreter reaches the end of the current code stack it's
currently executing. In your above test, loopTest() executes instantly
while nextTickTest() exeucutes later after the loop.
When people say process.nextTick() is faster, they are comparing it to
the common alternative of doing it in the browser which would be to do
setTimeout(func, 0)
https://gist.github.com/1508863
setTimeout result: 6156ms
nextTick result: 196ms
On Dec 21, 4:35 pm, Tristan <tristan.slomin...@gmail.com> wrote:
> Wow! #1 was a huge difference.
>
> I thought I took care of closures by generating them both in the loop and
> process.nextTick, but removing them per your response made a significant
> difference. This is much more along the lines of what I expected.
>
> Results using benchmark without closures:
> loop result: 22ms
> nextTick result: 178ms
>
> Benchmark without closures :https://gist.github.com/1508208
>
> Thanks Ben, that's a much more encouraging result.
--
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 nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
Uh?
Did you mean:
4µs * 1000000 = 4s
Or:
4ms * 1000 = 4s
?
---
Diogo R.
sorry,I'm wrong
Even in total absence of any events, calling a function in a while/for loop is always going to be much faster than looping via process.nextTick()s.
According to your benchmark it's only ~ 36 times faster, but the difference is even bigger than that, it seems to me.
Calling a function in a while (i--) { f() } loop, I get close to a hundred million calls per second, but only a mere half a million via nextTick()s. That's almost 200x faster.
Here's my benchmark: https://gist.github.com/1511972 and this is what I get in my Mac:
$ node nextTick.js
loopsPerSecond: 95419847.3, nextTicksPerSecond: 523953.4, ratio: 182.1x times faster
loopsPerSecond: 95785440.6, nextTicksPerSecond: 522615.9, ratio: 183.3x times faster
loopsPerSecond: 95969289.8, nextTicksPerSecond: 525575.9, ratio: 182.6x times faster
...
--
Jorge.
LOL. You get a *much* better nextTick rate !
What node was that ?
$ node -v
v0.6.6
--
Jorge.
> Huh... this is interesting, I was wondering why I crammed in more nextTicks than you.
>
> node v0.4.11-pre
> loopsPerSecond: 62266500.6, nextTicksPerSecond: 925533.3, ratio: 67.3x times faster
> node v0.6.6
> loopsPerSecond: 83963056.3, nextTicksPerSecond: 568846.9, ratio: 147.6x times faster
The nextTick() rate -almost- halved, really ?
--
Jorge.