Skip to content

Commit

Permalink
perf: use regexp in string escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko committed Oct 15, 2022
1 parent f5f1623 commit c6408cf
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 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) {
Expand All @@ -15,6 +18,9 @@ module.exports = class Serializer {
default:
this.parseInteger = Math.trunc
break

// eslint-disable-next-line
const strEscapeSequencesReplacer = new RegExp(strEscapeSequencesRegExp, 'g')
}
}

Expand Down Expand Up @@ -94,6 +100,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 {
Expand Down

0 comments on commit c6408cf

Please sign in to comment.