stream: improve Writable.destroy performance

Avoid nextTick if there are no pending callbacks.

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Robert Nagy
2020-09-05 13:50:26 +02:00
committed by Rich Trott
parent e06037abc4
commit e0d3b758a0

View File

@@ -45,6 +45,7 @@ const {
getHighWaterMark,
getDefaultHighWaterMark
} = require('internal/streams/state');
const assert = require('internal/assert');
const {
ERR_INVALID_ARG_TYPE,
ERR_METHOD_NOT_IMPLEMENTED,
@@ -826,9 +827,16 @@ ObjectDefineProperties(Writable.prototype, {
const destroy = destroyImpl.destroy;
Writable.prototype.destroy = function(err, cb) {
const state = this._writableState;
if (!state.destroyed) {
// Invoke pending callbacks.
if (
state.bufferedIndex < state.buffered.length ||
state[kOnFinished].length
) {
assert(!state.destroyed);
process.nextTick(errorBuffer, state);
}
destroy.call(this, err, cb);
return this;
};