Skip to content

Commit 9fac791

Browse files
authored
perf: improve isomorphic decode performance (nodejs#1791)
1 parent 286ffea commit 9fac791

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

lib/fetch/util.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,8 @@ function isReadableStreamLike (stream) {
863863
)
864864
}
865865

866+
const MAXIMUM_ARGUMENT_LENGTH = 65535
867+
866868
/**
867869
* @see https://infra.spec.whatwg.org/#isomorphic-decode
868870
* @param {number[]|Uint8Array} input
@@ -871,13 +873,12 @@ function isomorphicDecode (input) {
871873
// 1. To isomorphic decode a byte sequence input, return a string whose code point
872874
// length is equal to input’s length and whose code points have the same values
873875
// as the values of input’s bytes, in the same order.
874-
let output = ''
875876

876-
for (let i = 0; i < input.length; i++) {
877-
output += String.fromCharCode(input[i])
877+
if (input.length < MAXIMUM_ARGUMENT_LENGTH) {
878+
return String.fromCharCode(...input)
878879
}
879880

880-
return output
881+
return input.reduce((previous, current) => previous + String.fromCharCode(current), '')
881882
}
882883

883884
/**

0 commit comments

Comments
 (0)