Created
June 29, 2021 20:30
-
-
Save isaacs/ff35e6e06ea17c4bc235891b1e0c20a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const doFlush = process.argv.includes('flush') | |
const child = () => { | |
const N = 1e6 | |
for (let i = 0; i < N; i++) { | |
const arr = [i,i,i,i,i,i,i,i,i,i] | |
const a = arr.toString() + ' ' + arr.length | |
console.log(a) | |
} | |
if (doFlush) | |
process.stdout.write('', () => process.exit()) | |
else | |
process.exit() | |
} | |
const parent = () => { | |
const { spawn } = require('child_process') | |
const child = spawn(process.execPath, [__filename, 'child', doFlush ? 'flush' : 'noflush']) | |
let lastChunk = null | |
child.stdout.on('data', c => lastChunk = c.toString()) | |
child.stdout.on('close', () => console.log(lastChunk.slice(-100))) | |
} | |
if (process.argv.includes('child')) | |
child() | |
else | |
parent() | |
/* | |
$ node flush-stdout.js | |
287,3287,3287,3287,3287,3287,3287,3287,3287 10 | |
3288,3288,3288,3288,3288,3288,3288,3288,3288,3288 10 | |
$ node flush-stdout.js flush | |
999997,999997,999997,999997,999997,999997 10 | |
999998,999998,999998,999998,999998,999998,999998,999998,999998,999998 10 | |
999999,999999,999999,999999,999999,999999,999999,999999,999999,999999 10 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment