Skip to content

Commit cde0f7b

Browse files
committed
perf: simplify utf8toString Hermes non-ascii codepath
1 parent 2b99d3a commit cde0f7b

File tree

1 file changed

+3
-47
lines changed

1 file changed

+3
-47
lines changed

utf8.js

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,6 @@ function encode(str, loose = false) {
5252
return js.encode(str, loose)
5353
}
5454

55-
let esc
56-
57-
// This function is used only in escape + decodeURIComponent path, i.e. on Hermes
58-
function toEscapesPart(arr, start, end) {
59-
let o = ''
60-
let i = start
61-
// Unrolled loop is faster
62-
for (const last7 = end - 7; i < last7; i += 8) {
63-
const a = arr[i]
64-
const b = arr[i + 1]
65-
const c = arr[i + 2]
66-
const d = arr[i + 3]
67-
const e = arr[i + 4]
68-
const f = arr[i + 5]
69-
const g = arr[i + 6]
70-
const h = arr[i + 7]
71-
o += `${esc[a]}${esc[b]}${esc[c]}${esc[d]}${esc[e]}${esc[f]}${esc[g]}${esc[h]}` // templates are faster on Hermes
72-
}
73-
74-
while (i < end) o += esc[arr[i++]]
75-
return o
76-
}
77-
7855
function decode(arr, loose = false) {
7956
assertUint8(arr)
8057
if (haveDecoder) return loose ? decoderLoose.decode(arr) : decoderFatal.decode(arr) // Node.js and browsers
@@ -84,32 +61,11 @@ function decode(arr, loose = false) {
8461
const prefix = ascii.decode(arr, 0, ascii.asciiPrefix(arr))
8562
if (prefix.length === arr.length) return prefix
8663

87-
// This codepath gives a ~2x perf boost on Hermes
64+
// This codepath gives a ~3x perf boost on Hermes
8865
if (shouldUseEscapePath && escape && decodeURIComponent) {
89-
if (!esc) {
90-
const maybeEscape = (s, i) => i < 128 && s !== '%' ? s : escape(s) // don't escape ascii except the escape sign %
91-
esc = Array.from({ length: 256 }, (_, i) => String.fromCharCode(i)).map(maybeEscape)
92-
}
93-
const length = arr.length
94-
let o
95-
if (length - prefix.length > 30_000) {
96-
// Limit concatenation to avoid excessive GC
97-
// TODO: recheck thresholds on Hermes (taken from hex)
98-
const concat = []
99-
for (let i = prefix.length; i < length; ) {
100-
const i1 = Math.min(length, i + 500)
101-
concat.push(toEscapesPart(arr, i, i1))
102-
i = i1
103-
}
104-
105-
o = concat.join('')
106-
concat.length = 0
107-
} else {
108-
o = toEscapesPart(arr, prefix.length, length)
109-
}
110-
66+
const o = escape(ascii.decode(arr, prefix.length, arr.length))
11167
try {
112-
return prefix + decodeURIComponent(o) // ascii to utf8, escape() is precalculated
68+
return prefix + decodeURIComponent(o) // Latin1 to utf8
11369
} catch {
11470
if (!loose) throw new TypeError(E_STRICT)
11571
// Ok, we have to use manual implementation for loose decoder

0 commit comments

Comments
 (0)