From 2b443beab28dd42061e8e52dc554cb30a0a1f847 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 14 Jan 2022 16:37:45 -0800 Subject: [PATCH] stream: avoid function call where possible Instead of assigning a boolean, move the function call that assigns a value to the boolean to the only place that boolean is checked. This avoids the function call in cases where it is not needed. Refs: https://github.com/nodejs/node/pull/41488#pullrequestreview-850626528 --- lib/internal/streams/utils.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/internal/streams/utils.js b/lib/internal/streams/utils.js index d35744f26b7980..4d4f00ab456fa7 100644 --- a/lib/internal/streams/utils.js +++ b/lib/internal/streams/utils.js @@ -118,17 +118,19 @@ function isReadableFinished(stream, strict) { function isReadable(stream) { if (stream && stream[kIsReadable] != null) return stream[kIsReadable]; - const r = isReadableNodeStream(stream); if (typeof stream?.readable !== 'boolean') return null; if (isDestroyed(stream)) return false; - return r && stream.readable && !isReadableFinished(stream); + return isReadableNodeStream(stream) && + stream.readable && + !isReadableFinished(stream); } function isWritable(stream) { - const r = isWritableNodeStream(stream); if (typeof stream?.writable !== 'boolean') return null; if (isDestroyed(stream)) return false; - return r && stream.writable && !isWritableEnded(stream); + return isWritableNodeStream(stream) && + stream.writable && + !isWritableEnded(stream); } function isFinished(stream, opts) {