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 nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/3101820e-7425-42b2-b001-3a173c2fbd68%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/CAPJ5V2anFdsq8XQKDnu10H150so4qeaq8gVfBZPHUVtz8VbtFA%40mail.gmail.com.
function outer(a) {
function inner(arg1,arg2){
return arg1 + arg2;
}
return inner(a, 6);
}
myVar = outer(4);
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');--
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 nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/7d71682a-b81e-4d5f-95dc-629bb0eb5520%40googlegroups.com.
Interesting topicAbout"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)"I expected
in the other two examples the callback creation code is part of the argument list and COMPILED once, bind ONCE to any present closure, give it to the server logic, and it is invoked in EACH request
About"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."I expected
In the first example you create an inner function in the body of the outer, BUT IT is compiled ONCE, as the outer one. Then, at invocation of the outer, the ALREADY compiled inner function is bind to any present closure (I guess that a clever compiler could identify that the inner function has no reference to free variables, only to locals and arguments, so no "external" closure is needed)
The first example was
function outer(a) { function inner(arg1,arg2){ return arg1 + arg2; } return inner(a, 6); } myVar = outer(4);The server example washttp.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, '127.0.0.1');Is this the case?