It is created only once. However, if you define function in a closure - it'll be allocated each time the closure is executed.
On Sun, Aug 10, 2014 at 6:13 PM, David J Lima <[email protected]> wrote: > 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/3101820e-7425-42b2-b001-3a173c2fbd68%40googlegroups.com > <https://groups.google.com/d/msgid/nodejs/3101820e-7425-42b2-b001-3a173c2fbd68%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- 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/CAEv2VfJ_fg%2B1DX-AzVoJ%2Bjm2jusawo4S0X-iYfZM8b6E1kOhzw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
