3 ms·
Random thought/question, does `process.stdout.write("Hello World");` in Node.js make any difference? While `console.log()` is correct for this analysis since it
by franciscop 7y ago
Random thought/question, does `process.stdout.write("Hello World");` in Node.js make any difference? While `console.log()` is correct for this analysis since it's the more common one, it does a lot of extra internal logic: https://github.com/nodejs/node/blob/v13.x/lib/internal/console/constructor.js https://github.com/nodejs/node/blob/v13.x/lib/internal/conso...
Edit: I'm just curious and don't know how to even start testinng this, not trying to promote/demote Node.js in any way.
- np_tedious 7y agoIf it is any better, it probably would be the more fair entry (and perhaps similar for python and stdout/bytes) since Go's example did basically that instead of fmt.print with a string
- zamadatix 7y agoI would be surprised if console.log() made many more syscalls than process.stdout.write. More function calls probably but those aren't being counted and neither is RAM usage. "strace" would let you count and find out though! The size would be a few bytes larger since node is scripted and that's more characters.
- TheNorthman 7y agoI'm not able to reproduce the authors results (nor reliably ANY result) but you're right. Using `strace -c node -e <code>` I get ~630 syscalls using `console.log("Hello world")` and ~620 syscalls using `process.stdout.write("Hello World");`. This is on nodejs v13.5.0 EDIT: My previous comment was made using an old version of nodejs, the update halved the number of syscalls from being around ~1350
- z3t4 7y agoAs a performance optimization I use stderr instead of console.log, for some reason stderr is much faster then stdout :P And console.log is slow as hell, mostly because it's sync, but also because it does a ton of stuff; parsing the parameters, etc.
- m45t3r 7y agoIt is not that stderr is faster, it is that it is unbuffered by default (while stdout is buffered by default). If you flush your output after printing to stdout, you should get a similar result. And well, I am thinking you're talking about "much faster" for small amount of text (small enough to not fill the stdout buffer). Actually printing huge amount of text to stdout should be much faster than printing to stderr exactly because of this buffer.
- overgard 7y agoIf you're micro-optimizing javascript, you're already doomed.