Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created June 29, 2021 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/ff35e6e06ea17c4bc235891b1e0c20a9 to your computer and use it in GitHub Desktop.
Save isaacs/ff35e6e06ea17c4bc235891b1e0c20a9 to your computer and use it in GitHub Desktop.
#!/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