Skip to content

Commit

Permalink
perf: avoid replace
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Dec 1, 2023
1 parent 99a9f09 commit cd93cdb
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/fetch/dataURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,17 +412,20 @@ function forgivingBase64 (data) {
// 1. Remove all ASCII whitespace from data.
data = data.replace(ASCII_WHITESPACE_REPLACE_REGEX, '') // eslint-disable-line

let dataLength = data.length
// 2. If data’s code point length divides by 4 leaving
// no remainder, then:
if (data.length % 4 === 0) {
if (dataLength % 4 === 0) {
// 1. If data ends with one or two U+003D (=) code points,
// then remove them from data.
data = data.replace(/=?=$/, '')
if (data.charCodeAt(dataLength - 1) === 0x003D && data.charCodeAt(--dataLength) === 0x003D) {
--dataLength
}
}

// 3. If data’s code point length divides by 4 leaving
// a remainder of 1, then return failure.
if (data.length % 4 === 1) {
if (dataLength % 4 === 1) {
return 'failure'
}

Expand All @@ -431,7 +434,7 @@ function forgivingBase64 (data) {
// U+002F (/)
// ASCII alphanumeric
// then return failure.
if (/[^+/0-9A-Za-z]/.test(data)) {
if (/[^+/0-9A-Za-z]/.test(data.length === dataLength ? data : (data = data.substring(0, dataLength)))) {
return 'failure'
}

Expand Down

0 comments on commit cd93cdb

Please sign in to comment.