Skip to content

Commit 357c1e3

Browse files
perf: use regexp in string escaping (#553)
* perf: use regexp in string escaping * Add code comments for regexp check
1 parent f5f1623 commit 357c1e3

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

lib/serializer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict'
22

3+
// eslint-disable-next-line
4+
const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/
5+
36
module.exports = class Serializer {
47
constructor (options = {}) {
58
switch (options.rounding) {
@@ -94,6 +97,11 @@ module.exports = class Serializer {
9497
str = str.toString()
9598
}
9699

100+
// Fast escape chars check
101+
if (!STR_ESCAPE.test(str)) {
102+
return quotes + str + quotes
103+
}
104+
97105
if (str.length < 42) {
98106
return this.asStringSmall(str)
99107
} else {

0 commit comments

Comments
 (0)