From bf3b31ffe5b4fe5048e578ad8405b884e5b905a9 Mon Sep 17 00:00:00 2001 From: francesco Date: Wed, 13 Mar 2024 10:12:25 +0100 Subject: [PATCH] chore(perf): reduce if statement call on `"` and `\` 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 --- lib/serializer.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/serializer.js b/lib/serializer.js index 387e8927..f5830da8 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -128,10 +128,6 @@ 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 // '\' @@ -139,6 +135,9 @@ module.exports = class Serializer { 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) } }