Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does `console.log` buffer output in node.js?

I wonder if console.log buffers output in node.js or tries to perform IO on each invocation? This doesn't seem to be officially documented.

The question arose from the necessity to output an array of strings and I think which idiom is more efficient:

array.forEach(function(line){
  console.log(line)
})

or

console.log(array.join('\n'))

Thanks

like image 949
Alex Yursha Avatar asked Dec 17 '14 15:12

Alex Yursha


People also ask

Does console log buffer?

console. log() function is used to print the Buffer instance. It creates a buffer and allocates size to it. It initializes the buffer with given data.

Does console log slow down Nodejs?

As stderr is always written to synchronously, therefore in node. js any use of console. error, or other functions that write to stderr, will block your process until the output has all been written. The method is useful for error messages, but excessive use could slow down your process.

What does console log do in node JS?

log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.

Is console log Async Nodejs?

Console. log is asynchronous in windows while it is synchronous in linux/mac. To make console. log synchronous in windows write this line at the start of your code probably in index.


1 Answers

The documentation for console.log() probably doesn't specify whether it buffers output because it delegates that decision to an underlying stream:

Console.prototype.log = function() {
  this._stdout.write(util.format.apply(this, arguments) + '\n');
};

The writable.write() it's using documents the general possibility of buffering:

The return value indicates if you should continue writing right now. If the data had to be buffered internally, then it will return false. Otherwise, it will return true.

This return value is strictly advisory. You MAY continue to write, even if it returns false. However, writes will be buffered in memory, so it is best not to do this excessively. Instead, wait for the drain event before writing more data.

Though, the global console uses process.stdout that's more likely to block, buffering only under certain circumstances:

process.stderr and process.stdout are unlike other streams in Node in that writes to them are usually blocking.

  • They are blocking in the case that they refer to regular files or TTY file descriptors.
  • In the case they refer to pipes:
    • They are blocking in Linux/Unix.
    • They are non-blocking like other streams in Windows.

To see whether any lines are buffered, you could .write() them to stdout yourself and capture the return value:

var util = require('util');
var buffered = [];

array.forEach(function(line){
  buffered.push(!process.stdout.write(util.format(line) + '\n'));
});

console.log(buffered);
like image 162
Jonathan Lonowski Avatar answered Oct 08 '22 23:10

Jonathan Lonowski