The nature of callbacks in Node.js

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:

function outer(a) {
    function inner(arg1,arg2){
        return arg1 + arg2;
    }
    return inner(a, 6);
}
myVar = outer(4);

Apparently the inner function is garbage collected and the constant re-creation of it has a cost on performance. This could be re-factored as:

function inner(arg1, arg2) {
    return arg1 + arg2;
}

function outer(a) {
    return inner(a, 6);
}

myVar = outer(4);

If I am correct it seems that the tradeoff lies in the changing scope of inner; the fact that function inner no longer has access to outer’s own variables. If I am correct so far, lets move to Node.JS. Node is heavy on callbacks, and my question is, are asynchronous callback functions typical in Node also “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 Worldn');
}).listen(1337, '127.0.0.1');

In reality the function can get pretty large; it can encompass a lot of functionality. 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 in the plain javaScript example:

function handleGET(req, res) {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.end('Hello Worldn');
}

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);

0

http.createServer(function (req, res)
{
    // do stuff
});

is almost exactly the same as

function handler (req, res)
{
    // do stuff
}

http.createServer(handler);

The arguments passed to a called function are evaluated in the same scope as the call. Now, if you had done something like:

http.createServer(function (req, res)
{ 
  function handler (req, res)
  {
    // do stuff
  }
  return handler(req, res);
});

Then yes, you’d be creating a new function for every request, and that would be inefficient. In terms of something like a web server this is usually not a problem since the request handlers are often declared in a single place.

The only time you really want to nest functions (which is quite different from passing a function as an argument, as you are doing in your examples), is when you need references to the enclosing scope’s variables (ie: a closure)

In answer to your second question, no. There’s nothing different about event handler functions, and both your examples are functionally equivalent, and should essentially compile to the same machine code.

2

Node.JS doesn’t fundamentally change the way Javascript works, and the issue you’re describing is a Javascript issue.

More specifically, because the inner function is inside the scope of an outer function, it is recreated every time the inner function enters and leaves the outer scope. It’s exactly the same thing that happens when you declare a variable inside of a function, as opposed to outside of it. Node.JS has nothing to say about all that.

Fundamentally, callback functions are just functions; they have no special property that changes the way they behave in local scope.

Related
JSlint error ‘Don’t make functions within a loop.’

0

Note that a modern JavaScript implementation will only create individual function objects for different instances of the same function, these objects can easily share the code.

Both of your first two examples should, at least in V8, have the inner function inlined by the optimizer, thus effectively removing the overhead of having a second function at all. I’ll try to make time for some benchmarks, but for now I note that the first example could potentially be faster because it can be verified by simple static analysis that the inner function will never change. The second example has to be able to deal with the possibility that the global variable inner changes value.

You give the example code:

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello Worldn');
}).listen(1337, '127.0.0.1');

Note that this is not an example of multiple instances of the same function, createServer is called once and is in this process given the only instance of the anonymous function that is ever created. It is likely to call the anonymous function multiple times, but that is a different matter.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật