Is there a Node.js console.log length limit?

SkippyFlipjack picture SkippyFlipjack · Nov 19, 2014 · Viewed 7.9k times · Source

Is there a limit the length of console.log output in Node.js? The following prints numbers up to 56462, then stops. This came up because we were returning datasets from MySQL and the output would just quit after 327k characters.

var out = ""; 
for (i = 0; i < 100000; i++) {
    out += " " + i; 
}

console.log(out); 

The string itself seems fine, as this returns the last few numbers up to 99999:

console.log(out.substring(out.length - 23)); 

Returns:

99996 99997 99998 99999

This is using Node v0.6.14.

Answer

Arseny Smoogly picture Arseny Smoogly · Nov 19, 2014

Have you tried writing that much on a machine with more memory?
According to Node source code console is writing into a stream: https://github.com/joyent/node/blob/cfcb1de130867197cbc9c6012b7e84e08e53d032/lib/console.js#L55
And streams may buffer the data into memory: http://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback

So if you put reeeaally a lot of data into a stream, you may hit the memory ceiling. I'd recommend you split up your data and feed it into process.stdout.write method, here's an example: http://nodejs.org/api/stream.html#stream_event_drain