From 130b53e248cf5069f13a2fdce381d38266dbf275 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Sat, 30 Dec 2023 11:59:46 +0900 Subject: [PATCH] perf: Improve percentDecode --- lib/fetch/dataURL.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/fetch/dataURL.js b/lib/fetch/dataURL.js index 762017c8cc9..12fa1abbb93 100644 --- a/lib/fetch/dataURL.js +++ b/lib/fetch/dataURL.js @@ -188,11 +188,28 @@ function stringPercentDecode (input) { return percentDecode(bytes) } +/** + * @param {number} byte + */ function isHexCharByte (byte) { // 0-9 A-F a-f return (byte >= 0x30 && byte <= 0x39) || (byte >= 0x41 && byte <= 0x46) || (byte >= 0x61 && byte <= 0x66) } +/** + * @param {number} byte + */ +function hexByteToNumber (byte) { + return ( + // 0-9 + byte >= 0x30 && byte <= 0x39 + ? (byte - 48) + // Convert to uppercase + // ((byte & 0xDF) - 65) + 10 + : ((byte & 0xDF) - 55) + ) +} + // https://url.spec.whatwg.org/#percent-decode /** @param {Uint8Array} input */ function percentDecode (input) { @@ -224,11 +241,8 @@ function percentDecode (input) { } else { // 1. Let bytePoint be the two bytes after byte in input, // decoded, and then interpreted as hexadecimal number. - const nextTwoBytes = String.fromCharCode(input[i + 1], input[i + 2]) - const bytePoint = Number.parseInt(nextTwoBytes, 16) - // 2. Append a byte whose value is bytePoint to output. - output[j++] = bytePoint + output[j++] = (hexByteToNumber(input[i + 1]) << 4) | hexByteToNumber(input[i + 2]) // 3. Skip the next two bytes in input. i += 2