Skip to content

Commit c650966

Browse files
UzlopakRafaelGSS
andauthored
improve asString performance (#592)
* improve asString performance * Update lib/serializer.js Co-authored-by: Rafael Gonzaga <rafael.nunu@hotmail.com> * fix lint issue Co-authored-by: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent 9c5321a commit c650966

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

lib/serializer.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,24 @@ module.exports = class Serializer {
9898
}
9999

100100
asString (str) {
101-
const quotes = '"'
102-
if (str instanceof Date) {
103-
return quotes + str.toISOString() + quotes
104-
} else if (str === null) {
105-
return '""'
106-
} else if (str instanceof RegExp) {
107-
str = str.source
108-
} else if (typeof str !== 'string') {
109-
str = str.toString()
101+
if (typeof str !== 'string') {
102+
if (str === null) {
103+
return '""'
104+
}
105+
if (str instanceof Date) {
106+
return '"' + str.toISOString() + '"'
107+
}
108+
if (str instanceof RegExp) {
109+
str = str.source
110+
} else {
111+
str = str.toString()
112+
}
110113
}
111114

112115
// Fast escape chars check
113116
if (!STR_ESCAPE.test(str)) {
114-
return quotes + str + quotes
115-
}
116-
117-
if (str.length < 42) {
117+
return '"' + str + '"'
118+
} else if (str.length < 42) {
118119
return this.asStringSmall(str)
119120
} else {
120121
return JSON.stringify(str)

0 commit comments

Comments
 (0)