Skip to content

Commit

Permalink
chore(perf): reduce if statement call on " and \
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
cesco69 authored Mar 13, 2024
1 parent a63ed55 commit bf3b31f
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,16 @@ module.exports = class Serializer {
// eslint-disable-next-line
for (var i = 0; i < len; i++) {
point = str.charCodeAt(i)
if (point < 32 || (point >= 0xD800 && point <= 0xDFFF)) {
// The current character is non-printable characters or a surrogate.
return JSON.stringify(str)
}
if (
point === 0x22 || // '"'
point === 0x5c // '\'
) {
last === -1 && (last = 0)
result += str.slice(last, i) + '\\'
last = i
} else if (point < 32 || (point >= 0xD800 && point <= 0xDFFF)) {
// The current character is non-printable characters or a surrogate.
return JSON.stringify(str)
}
}

Expand Down

0 comments on commit bf3b31f

Please sign in to comment.