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 c683091
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 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 Down Expand Up @@ -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 {
Expand Down

0 comments on commit c683091

Please sign in to comment.