From c6830917714c358c4ed7d4b1bf8f9a6d08fb29c2 Mon Sep 17 00:00:00 2001 From: Ivan Tymoshenko Date: Sat, 15 Oct 2022 14:33:54 +0300 Subject: [PATCH 1/2] perf: use regexp in string escaping --- lib/serializer.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/serializer.js b/lib/serializer.js index 3157b2ec..fca01a29 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -1,5 +1,8 @@ 'use strict' +// eslint-disable-next-line +const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/ + module.exports = class Serializer { constructor (options = {}) { switch (options.rounding) { @@ -94,6 +97,10 @@ module.exports = class Serializer { str = str.toString() } + if (!STR_ESCAPE.test(str)) { + return quotes + str + quotes + } + if (str.length < 42) { return this.asStringSmall(str) } else { From 87f59021c20847cd0f81bec7a1803675bfbc6135 Mon Sep 17 00:00:00 2001 From: Ivan Tymoshenko Date: Sat, 15 Oct 2022 15:55:49 +0300 Subject: [PATCH 2/2] Add code comments for regexp check --- lib/serializer.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/serializer.js b/lib/serializer.js index fca01a29..cd0d1da3 100644 --- a/lib/serializer.js +++ b/lib/serializer.js @@ -97,6 +97,7 @@ module.exports = class Serializer { str = str.toString() } + // Fast escape chars check if (!STR_ESCAPE.test(str)) { return quotes + str + quotes }