Skip to content

Commit 9bfddf6

Browse files
tsctxcrysmags
authored andcommitted
perf: Improve percentDecode (nodejs#2562)
1 parent 10e47c8 commit 9bfddf6

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

lib/fetch/dataURL.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,28 @@ function stringPercentDecode (input) {
188188
return percentDecode(bytes)
189189
}
190190

191+
/**
192+
* @param {number} byte
193+
*/
191194
function isHexCharByte (byte) {
192195
// 0-9 A-F a-f
193196
return (byte >= 0x30 && byte <= 0x39) || (byte >= 0x41 && byte <= 0x46) || (byte >= 0x61 && byte <= 0x66)
194197
}
195198

199+
/**
200+
* @param {number} byte
201+
*/
202+
function hexByteToNumber (byte) {
203+
return (
204+
// 0-9
205+
byte >= 0x30 && byte <= 0x39
206+
? (byte - 48)
207+
// Convert to uppercase
208+
// ((byte & 0xDF) - 65) + 10
209+
: ((byte & 0xDF) - 55)
210+
)
211+
}
212+
196213
// https://url.spec.whatwg.org/#percent-decode
197214
/** @param {Uint8Array} input */
198215
function percentDecode (input) {
@@ -224,11 +241,8 @@ function percentDecode (input) {
224241
} else {
225242
// 1. Let bytePoint be the two bytes after byte in input,
226243
// decoded, and then interpreted as hexadecimal number.
227-
const nextTwoBytes = String.fromCharCode(input[i + 1], input[i + 2])
228-
const bytePoint = Number.parseInt(nextTwoBytes, 16)
229-
230244
// 2. Append a byte whose value is bytePoint to output.
231-
output[j++] = bytePoint
245+
output[j++] = (hexByteToNumber(input[i + 1]) << 4) | hexByteToNumber(input[i + 2])
232246

233247
// 3. Skip the next two bytes in input.
234248
i += 2

0 commit comments

Comments
 (0)