10

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

Alex Yursha
  • 3,208
  • 3
  • 26
  • 25
  • It's hard to imagine the `forEach` method would be faster unless you already do it for something else, but the core question is interesting. +1 – Alexander O'Mara Dec 17 '14 at 15:44
  • Well, in the second approach you allocate memory to the possibly big string up front, then send it all to `console.log`, which may or not have a buffer, using even more memory. In this case, the best option is to look at the source code – This company is turning evil. Dec 17 '14 at 15:54
  • @Kroltan, looking at the sources is the best approach if you have enough time and expertise. But what if you don't? – Alex Yursha Dec 17 '14 at 16:02
  • Alex, sorry. The source for [console.js](https://github.com/joyent/node/blob/master/lib/console.js): Looking at the `log` function, it just prints the value + a newline. And the default behavior for that in the underlying C/C++ library is that it doesn't flush except when necessary, so yes, most of the time it buffers. – This company is turning evil. Dec 17 '14 at 16:06
  • 2
    Very much related, if not duplicate: [is node.js' console.log asynchronous?](http://stackoverflow.com/q/5127532/1048572) (and maybe [Difference between “process.stdout.write” and “console.log” in node.js?](http://stackoverflow.com/q/4976466/1048572)) – Bergi Dec 17 '14 at 16:06

1 Answers1

7

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);
Community
  • 1
  • 1
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • Isn't the question then whether `this._stdout` *does* actually buffer? – Bergi Dec 17 '14 at 16:09
  • @Bergi Sure, but it's not a simple "yes" or "no." It can buffer, but isn't guaranteed to. – Jonathan Lonowski Dec 17 '14 at 16:10
  • Writables in general can be both, yes, but stdout is a very specific instance. And it's [known to be blocking](http://nodejs.org/api/process.html#process_process_stdout) – Bergi Dec 17 '14 at 16:15