Skip to content

Commit

Permalink
improve asString performance (#592)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Uzlopak and RafaelGSS committed Jan 22, 2023
1 parent 9c5321a commit c650966
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,24 @@ module.exports = class Serializer {
}

asString (str) {
const quotes = '"'
if (str instanceof Date) {
return quotes + str.toISOString() + quotes
} else if (str === null) {
return '""'
} else if (str instanceof RegExp) {
str = str.source
} else if (typeof str !== 'string') {
str = str.toString()
if (typeof str !== 'string') {
if (str === null) {
return '""'
}
if (str instanceof Date) {
return '"' + str.toISOString() + '"'
}
if (str instanceof RegExp) {
str = str.source
} else {
str = str.toString()
}
}

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

if (str.length < 42) {
return '"' + str + '"'
} else if (str.length < 42) {
return this.asStringSmall(str)
} else {
return JSON.stringify(str)
Expand Down

0 comments on commit c650966

Please sign in to comment.