Skip to content

Commit

Permalink
stream: improve Writable.destroy performance
Browse files Browse the repository at this point in the history
Avoid nextTick if there are no pending callbacks.

PR-URL: nodejs#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>
  • Loading branch information
ronag authored and joesepi committed Oct 22, 2020
1 parent 2f75de4 commit 079af89
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit 079af89

Please sign in to comment.