The first example with outer and inner functions and the other two are 
different. in the first example you create an inner function in the body of 
the outer, so the creation code gets executed every time the outer function 
is.

in the other two examples the callback creation code is part of the 
argument list and executed once (as far as you don't create servers in 
series of course)

Am Sonntag, 10. August 2014 16:13:37 UTC+2 schrieb David J Lima:
>
> In the plain javaScript world I've learned that nested functions can be 
> problematic because the inner function is "re-created" every time the outer 
> function is called. Apparently the inner function is garbage collected and 
> the constant re-creation of it has a cost on performance.
>
> function outer(a) {
>     function inner(arg1,arg2){
>         return arg1 + arg2;
>     }
>     return inner(a, 6);
> }
> myVar = outer(4);
>
> Are asynchronous callback functions typical in Node "re-created" every 
> time they are fired? Take the following common Node construct:
>
> http.createServer(function (req, res) {
>     res.writeHead(200, {'Content-Type': 'text/plain'});
>     res.end('Hello World\n');
> }).listen(1337, '127.0.0.1');
>
> Is the above callback "re-created" every time a client GETs or POSTs the 
> server? If so, would it be better to always de-couple the callbacks as :
>
> function handleGET(req, res) {
>    res.writeHead(200, {'Content-Type': 'text/plain'});
>    res.end('Hello World\n');
> }
>
> http.createServer(handleGET).listen(1337, '127.0.0.1');
>
> as a related bonus question, are event functions re-created too? Would 
> they benefit from prior declaration?
>
> myObj.on('someEvent', function someFunction() {...});
>
> vs.
>
> function someFunction() {...}
> myObj.on('someEvent', someFunction);
>
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/7d71682a-b81e-4d5f-95dc-629bb0eb5520%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to