Skip to content

Commit

Permalink
stream: use fast-path for only large inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Nov 27, 2022
1 parent 2661702 commit 9ecac0b
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/internal/streams/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
);
}

Expand Down

0 comments on commit 9ecac0b

Please sign in to comment.