Skip to content

Commit b07e607

Browse files
authored
chore(perf): reduce if statement call on " and \ (#695)
With this PR when a `"` or `\` is into the string, the if statement for surrogate check is not called. This is a little performance improvment Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
1 parent 59ce4f7 commit b07e607

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

lib/serializer.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,16 @@ module.exports = class Serializer {
128128
// eslint-disable-next-line
129129
for (var i = 0; i < len; i++) {
130130
point = str.charCodeAt(i)
131-
if (point < 32 || (point >= 0xD800 && point <= 0xDFFF)) {
132-
// The current character is non-printable characters or a surrogate.
133-
return JSON.stringify(str)
134-
}
135131
if (
136132
point === 0x22 || // '"'
137133
point === 0x5c // '\'
138134
) {
139135
last === -1 && (last = 0)
140136
result += str.slice(last, i) + '\\'
141137
last = i
138+
} else if (point < 32 || (point >= 0xD800 && point <= 0xDFFF)) {
139+
// The current character is non-printable characters or a surrogate.
140+
return JSON.stringify(str)
142141
}
143142
}
144143

0 commit comments

Comments
 (0)