Skip to content

Commit bfc2bea

Browse files
committed
fix: revert to Uzlopak proposal, is more stable and faster on more cases on node 24
1 parent b287ce4 commit bfc2bea

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

lib/serializer.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,7 @@ module.exports = class Serializer {
9595

9696
if (len === 0) {
9797
return '""'
98-
}
99-
100-
if (!str.isWellFormed()) {
101-
return JSON.stringify(str)
102-
}
103-
104-
if (len < 42) {
98+
} else if (len < 42) {
10599
// magically escape strings for json
106100
// relying on their charCodeAt
107101
// everything below 32 needs JSON.stringify()
@@ -120,13 +114,13 @@ module.exports = class Serializer {
120114
last === -1 && (last = 0)
121115
result += str.slice(last, i) + '\\'
122116
last = i
123-
} else if (point < 32) {
117+
} else if (point < 32 || (point >= 0xd800 && point <= 0xdfff)) {
124118
// The current character is non-printable characters or a surrogate.
125119
return JSON.stringify(str)
126120
}
127121
}
128122
return (last === -1 && ('"' + str + '"')) || ('"' + result + str.slice(last) + '"')
129-
} else if (len < 5000 && ASCII_ESCAPE.test(str) === false) {
123+
} else if (len < 5000 && str.isWellFormed() && ASCII_ESCAPE.test(str) === false) {
130124
// Only use the regular expression for shorter input. The overhead is otherwise too much.
131125
return '"' + str + '"'
132126
} else {

0 commit comments

Comments
 (0)