From 9ecac0bd254606eae20a0bf2f627aacc614268e1 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 21 Nov 2022 19:03:53 -0500 Subject: [PATCH] stream: use fast-path for only large inputs --- lib/internal/streams/utils.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/internal/streams/utils.js b/lib/internal/streams/utils.js index 7904b4a66d1dec..bc58a006f105e3 100644 --- a/lib/internal/streams/utils.js +++ b/lib/internal/streams/utils.js @@ -12,6 +12,7 @@ const { const { normalizeEncoding } = require('internal/util'); const { FastBuffer } = require('internal/buffer'); const { TextEncoder } = require('internal/encoding'); +const { isTypedArray } = require('internal/util/types'); let buffer; function lazyLoadBuffer() { @@ -277,13 +278,14 @@ function isErrored(stream) { function encodeWithFastPath(input, encoding) { const enc = normalizeEncoding(encoding); + const byteLength = enc === 'utf8' && isTypedArray(input) && TypedArrayPrototypeGetByteLength(input); - if (enc === 'utf8') { + if (byteLength > 512) { const buf = encoder.encode(input); return new FastBuffer( TypedArrayPrototypeGetBuffer(buf), TypedArrayPrototypeGetByteOffset(buf), - TypedArrayPrototypeGetByteLength(buf), + byteLength, ); }