Skip to content

Commit

Permalink
Small perf improvement (5%) in sync mode (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Aug 13, 2020
1 parent d6deb50 commit a299047
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const MAX_WRITE = 16 * 1024 * 1024
function openFile (file, sonic) {
sonic._opening = true
sonic._writing = true
sonic._asyncDrainScheduled = false
sonic.file = file

// NOTE: 'error' and 'ready' events emitted below only relevant when sonic.sync===false
Expand Down Expand Up @@ -164,9 +165,17 @@ function SonicBoom (opts) {
}
}
}

this.on('newListener', function (name) {
if (name === 'drain') {
this._asyncDrainScheduled = false
}
})
}

function emitDrain (sonic) {
const hasListeners = sonic.listenerCount('drain') > 0
if (!hasListeners) return
sonic._asyncDrainScheduled = false
sonic.emit('drain')
}
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ function buildTests (test, sync) {
const fd = fs.openSync(dest, 'w')
const stream = new SonicBoom({ fd, sync })

t.ok(stream.write('hello world\n'))

stream.once('drain', () => {
fs.readFile(dest, 'utf8', (err, data) => {
t.error(err)
Expand All @@ -91,6 +89,8 @@ function buildTests (test, sync) {
})
})

t.ok(stream.write('hello world\n'))

stream.on('finish', () => {
t.pass('finish emitted')
})
Expand Down

0 comments on commit a299047

Please sign in to comment.