Skip to content

Commit

Permalink
perf: Improve percentDecode (#2562)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Dec 30, 2023
1 parent d432961 commit 35b049b
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/fetch/dataURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 35b049b

Please sign in to comment.