Skip to content

Commit 033037c

Browse files
ronagTrott
authored andcommitted
stream: avoid unecessary nextTick
If we are not going to emit 'close' then there is no reason to schedule it. PR-URL: #29194 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 0e715de commit 033037c

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

lib/internal/streams/destroy.js

+13-17
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,35 @@ function destroy(err, cb) {
4848
}
4949

5050
this._destroy(err || null, (err) => {
51+
const emitClose = (w && w.emitClose) || (r && r.emitClose);
5152
if (cb) {
52-
process.nextTick(emitCloseNT, this);
53+
if (emitClose) {
54+
process.nextTick(emitCloseNT, this);
55+
}
5356
cb(err);
5457
} else if (needError(this, err)) {
55-
process.nextTick(emitErrorAndCloseNT, this, err);
56-
} else {
58+
process.nextTick(emitClose ? emitErrorCloseNT : emitErrorNT, this, err);
59+
} else if (emitClose) {
5760
process.nextTick(emitCloseNT, this);
5861
}
5962
});
6063

6164
return this;
6265
}
6366

64-
function emitErrorAndCloseNT(self, err) {
65-
emitErrorNT(self, err);
66-
emitCloseNT(self);
67+
function emitErrorCloseNT(self, err) {
68+
self.emit('error', err);
69+
self.emit('close');
6770
}
6871

6972
function emitCloseNT(self) {
70-
const r = self._readableState;
71-
const w = self._writableState;
72-
73-
if (w && !w.emitClose)
74-
return;
75-
if (r && !r.emitClose)
76-
return;
7773
self.emit('close');
7874
}
7975

76+
function emitErrorNT(self, err) {
77+
self.emit('error', err);
78+
}
79+
8080
function undestroy() {
8181
const r = this._readableState;
8282
const w = this._writableState;
@@ -100,10 +100,6 @@ function undestroy() {
100100
}
101101
}
102102

103-
function emitErrorNT(self, err) {
104-
self.emit('error', err);
105-
}
106-
107103
function errorOrDestroy(stream, err) {
108104
// We have tests that rely on errors being emitted
109105
// in the same tick, so changing this is semver major.

0 commit comments

Comments
 (0)