I also initially preferred approach B "{callback();return}", but now use
approach A "return callback()". Yes there is a slight difference under
the hood, at least in the initial abstract syntax tree. Usually this is
done in an async callback where the return value is going to be ignored
anyway.
If any of the V8 developers could shed more light on how these two options
affect the optimizer or runtime performance that would be interesting.
I prefer the single statement because:
- It's a habit from languages with tail recursion. If you return
statement is a call to a function, it's more like a goto in cost than a
function call. JavaScript doesn't have this in any engine I'm aware of so
it's more a habit than anything. (also the return value is almost always
ignored or we wouldn't be having this discussion in the first place)
- It's less code. When in doubt go for less.
On Wed, Apr 11, 2012 at 9:29 AM, Mark Volkmann <[email protected]>wrote:
> On Tue, Apr 10, 2012 at 6:17 PM, Ken <[email protected]> wrote:
> > Assuming that the callback doesn't return a value, does v8 behave any
> > differently when invoking callbacks in one of these forms vs. the
> other? I
> > find the first approach to be a convenient shorthand in many cases, but
> am
> > wondering (after observing some unexpected timings when profiling async
> > methods) if it leads to v8 doing something odd with the stack.
> >
> > Approach A, return the invoked callback:
> >
> > function foo(a, callback) {
> > var bar = ...;
> > return callback(bar);
> > }
> >
> > foo("derp", function(b) { ...; return; });
> >
> > Approach B, invoke callback, then return:
> >
> > function foo(a, callback) {
> > var bar = ...;
> > callback(bar);
> > return;
> > }
> >
> > foo("derp", function(b) { ...; return; });
>
> Initially I really hated approach A, but it seems that approach is
> very common and it has grown on me. I always use approach A now.
>
> --
> R. Mark Volkmann
> Object Computing, Inc.
>
> --
> 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
>
--
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