Skip to content

Commit

Permalink
perf: improve isomorphic decode performance (nodejs#1791)
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Apr 4, 2023
1 parent 41fffdb commit fd4adc9
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,8 @@ function isReadableStreamLike (stream) {
)
}

const MAXIMUM_ARGUMENT_LENGTH = 65535

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

for (let i = 0; i < input.length; i++) {
output += String.fromCharCode(input[i])
if (input.length < MAXIMUM_ARGUMENT_LENGTH) {
return String.fromCharCode(...input)
}

return output
return input.reduce((previous, current) => previous + String.fromCharCode(current), '')
}

/**
Expand Down

0 comments on commit fd4adc9

Please sign in to comment.