Skip to content

Instantly share code, notes, and snippets.

@bcherny
Created February 4, 2019 02:15
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 bcherny/0f778173246356d8467112c0365b10a0 to your computer and use it in GitHub Desktop.
Save bcherny/0f778173246356d8467112c0365b10a0 to your computer and use it in GitHub Desktop.
let {readFile} = require('fs')
let tally = {ab: 0, ba: 0}
let globalDone = 0
let COUNT = 1000
for (let x = 0; x < COUNT; x++) {
let result = ''
let done = 0
let checkDone = () => {
done++
if (done === 2) {
globalDone++
tally[result]++
}
if (globalDone === COUNT) {
console.log(JSON.stringify(tally, null, 2))
}
}
readFile('./a.txt', {encoding: 'utf8'}, (error, data) => {
result += data
checkDone()
})
readFile('./b.txt', {encoding: 'utf8'}, (error, data) => {
result += data
checkDone()
})
}
/*
Output from a few runs:
$ node ./index.js
{
"ab": 969,
"ba": 31
}
{
"ab": 988,
"ba": 12
}
$ node ./index.js
{
"ab": 983,
"ba": 17
}
$ node ./index.js
{
"ab": 970,
"ba": 30
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment