Skip to content

Commit e0d3b75

Browse files
ronagTrott
authored andcommitted
stream: improve Writable.destroy performance
Avoid nextTick if there are no pending callbacks. PR-URL: #35067 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
1 parent e06037a commit e0d3b75

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/_stream_writable.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const {
4545
getHighWaterMark,
4646
getDefaultHighWaterMark
4747
} = require('internal/streams/state');
48+
const assert = require('internal/assert');
4849
const {
4950
ERR_INVALID_ARG_TYPE,
5051
ERR_METHOD_NOT_IMPLEMENTED,
@@ -826,9 +827,16 @@ ObjectDefineProperties(Writable.prototype, {
826827
const destroy = destroyImpl.destroy;
827828
Writable.prototype.destroy = function(err, cb) {
828829
const state = this._writableState;
829-
if (!state.destroyed) {
830+
831+
// Invoke pending callbacks.
832+
if (
833+
state.bufferedIndex < state.buffered.length ||
834+
state[kOnFinished].length
835+
) {
836+
assert(!state.destroyed);
830837
process.nextTick(errorBuffer, state);
831838
}
839+
832840
destroy.call(this, err, cb);
833841
return this;
834842
};

0 commit comments

Comments
 (0)