Skip to content

Commit fa90577

Browse files
committed
perf: 2x faster utf-8 deLoose in Hermes in worst-case
1 parent 2961d98 commit fa90577

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

utf8.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ function deLoose(str, loose, res) {
3535
start = pos + 1
3636
if (res[pos + 1] === 0xbf && res[pos + 2] === 0xbd) {
3737
// Found a replacement char in output, need to recheck if we encoded the input correctly
38-
if (str !== decode(res)) throw new TypeError(E_STRICT_UNICODE)
39-
return res
38+
if (!nativeDecoder && str.length < 1e7) {
39+
// This is ~2x faster than decode in Hermes
40+
try {
41+
if (encodeURI(str) !== null) return res // guard against optimizing out
42+
} catch {}
43+
} else if (str === decode(res)) return res
44+
throw new TypeError(E_STRICT_UNICODE)
4045
}
4146
}
4247

0 commit comments

Comments
 (0)