Question

Which makes more sense in terms of best practice for node.js?

This:

var http = require('http');
var server = http.createServer(function (request, response) {
        console.log('Hello World');
        console.log('Writing something')
        response.writeHead(200, "OK", {'Content-Type':'text/plain'});
        response.end('Hello World');
    });
server.listen(8888); 

or this:

var server = http.createServer(function (request, response) {
        console.log('Hello World');
        setImmediate(function() {
           console.log('Writing something');
           response.writeHead(200, "OK", {'Content-Type':'text/plain'});
           response.end('Hello World');
        });
    });
server.listen(8888); 

I'm asking because I wonder if you can go overboard with the event loop and callbacks. I'm finding myself constantly using callbacks and relying on the event loop to not block anything. Is that the right approach?

It seems to me that there is a trade off between the overhead associated with separating everything for event loop processing vs some blocking.

Should all I/O be processed separately through the event loop no matter how trivial it may seem? If not, where is a reasonable cutoff?

Was it helpful?

Solution

node.js takes care of this for you. All networking I/O in node without exception is done asynchronously and does not block the event loop. There is simply no synchronous networking API provided by node core. There is no need, and you should not, add additional calls to setImmediate, nextTick, or setTimeout as these will complicate your code and not provide any performance benefit.

The only time you need to hand-code yielding the event loop is in lengthy CPU processing such as cryptography, but all your standard DB-backed web site HTTP CRUD/REST operations and streaming data are perfectly fine to code without any explicit forcing of event loop ticks.

And as to synchronous IO for filesystem or child_processes, once you are running a network server, you should use the asynchronous calls exclusively. The synchronous calls are for initial application loading/startup and non-server command line, single-end-user scripts.

To clarify your example: this is NOT I/O:

response.writeHead(200, "OK", {'Content-Type':'text/plain'});

That is writing an in-memory string to an in-memory buffer which will be sent to libuv to be sent across the network by the OS. This is fast and does not require treatment as a slow asynchronous IO operation.

OTHER TIPS

I think the first example makes more sense, there is no need to delay the response to the client. setImmediate should be used when you need to process something that isn't "important" to the current event loop, let me give you a code example.

var server = http.createServer(function (request, response) {
        setImmediate(function() {
            //do some processing here that isn't necessary to the response
            //for ie: find the location by the ip address and save it somewhere
        });
        console.log('Writing something');
        response.writeHead(200, "OK", {'Content-Type':'text/plain'});
        response.end('Hello World');
    });
server.listen(8888); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top