From 4025c62f4ea70f05ebad3475c21b6add7ee06db7 Mon Sep 17 00:00:00 2001 From: SindreXie Date: Tue, 6 Sep 2022 15:58:31 +0800 Subject: [PATCH 1/2] stream: refactor use es2020 statement --- lib/internal/streams/destroy.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 5c352135dc3012..09cc5590fad4fc 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -42,7 +42,7 @@ function destroy(err, cb) { // With duplex streams we use the writable side for state. const s = w || r; - if ((w && w.destroyed) || (r && r.destroyed)) { + if (w?.destroyed || r?.destroyed) { if (typeof cb === 'function') { cb(); } @@ -128,7 +128,7 @@ function emitCloseNT(self) { r.closeEmitted = true; } - if ((w && w.emitClose) || (r && r.emitClose)) { + if (w?.emitClose || r?.emitClose) { self.emit('close'); } } @@ -137,7 +137,7 @@ function emitErrorNT(self, err) { const r = self._readableState; const w = self._writableState; - if ((w && w.errorEmitted) || (r && r.errorEmitted)) { + if (w?.errorEmitted || r?.errorEmitted) { return; } @@ -192,11 +192,11 @@ function errorOrDestroy(stream, err, sync) { const r = stream._readableState; const w = stream._writableState; - if ((w && w.destroyed) || (r && r.destroyed)) { + if (w?.destroyed || r?.destroyed) { return this; } - if ((r && r.autoDestroy) || (w && w.autoDestroy)) + if (r?.autoDestroy || w?.autoDestroy) stream.destroy(err); else if (err) { // Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364 @@ -283,7 +283,7 @@ function emitConstructNT(stream) { } function isRequest(stream) { - return stream && stream.setHeader && typeof stream.abort === 'function'; + return stream?.setHeader && typeof stream.abort === 'function'; } function emitCloseLegacy(stream) { From a5dd0c9c2bb704dd7d142683104f797816a73e2b Mon Sep 17 00:00:00 2001 From: SindreXie Date: Wed, 7 Sep 2022 11:11:29 +0800 Subject: [PATCH 2/2] benchmark: add stream destroy benchmark --- benchmark/streams/destroy.js | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 benchmark/streams/destroy.js diff --git a/benchmark/streams/destroy.js b/benchmark/streams/destroy.js new file mode 100644 index 00000000000000..c6811139d8c443 --- /dev/null +++ b/benchmark/streams/destroy.js @@ -0,0 +1,56 @@ +'use strict'; +const common = require('../common.js'); +const { + Duplex, + Readable, + Transform, + Writable, +} = require('stream'); + +const bench = common.createBenchmark(main, { + n: [1e6], + kind: ['duplex', 'readable', 'transform', 'writable'] +}); + +function main({ n, kind }) { + switch (kind) { + case 'duplex': + new Duplex({}); + new Duplex(); + + bench.start(); + for (let i = 0; i < n; ++i) + new Duplex().destroy(); + bench.end(n); + break; + case 'readable': + new Readable({}); + new Readable(); + + bench.start(); + for (let i = 0; i < n; ++i) + new Readable().destroy(); + bench.end(n); + break; + case 'writable': + new Writable({}); + new Writable(); + + bench.start(); + for (let i = 0; i < n; ++i) + new Writable().destroy(); + bench.end(n); + break; + case 'transform': + new Transform({}); + new Transform(); + + bench.start(); + for (let i = 0; i < n; ++i) + new Transform().destroy(); + bench.end(n); + break; + default: + throw new Error('Invalid kind'); + } +}