Skip to content

Commit

Permalink
perf: use regexp in string escaping (#553)
Browse files Browse the repository at this point in the history
* perf: use regexp in string escaping

* Add code comments for regexp check
  • Loading branch information
ivan-tymoshenko committed Oct 27, 2022
1 parent f5f1623 commit 357c1e3
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 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,11 @@ module.exports = class Serializer {
str = str.toString()
}

// Fast escape chars check
if (!STR_ESCAPE.test(str)) {
return quotes + str + quotes
}

if (str.length < 42) {
return this.asStringSmall(str)
} else {
Expand Down

0 comments on commit 357c1e3

Please sign in to comment.