Skip to content

Commit dfb627e

Browse files
authored
fix: function asString argument types for trigger V8 optimization (monomorphi) (#689)
1 parent 64e163d commit dfb627e

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,21 @@ function buildSingleTypeSerializer (context, location, input) {
728728
} else if (schema.format === 'unsafe') {
729729
return `json += serializer.asUnsafeString(${input})`
730730
} else {
731-
return `json += serializer.asString(${input})`
731+
return `
732+
if (typeof ${input} !== 'string') {
733+
if (${input} === null) {
734+
json += '""'
735+
} else if (${input} instanceof Date) {
736+
json += '"' + ${input}.toISOString() + '"'
737+
} else if (${input} instanceof RegExp) {
738+
json += serializer.asString(${input}.source)
739+
} else {
740+
json += serializer.asString(${input}.toString())
741+
}
742+
} else {
743+
json += serializer.asString(${input})
744+
}
745+
`
732746
}
733747
}
734748
case 'integer':

lib/serializer.js

-14
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,6 @@ module.exports = class Serializer {
9999
}
100100

101101
asString (str) {
102-
if (typeof str !== 'string') {
103-
if (str === null) {
104-
return '""'
105-
}
106-
if (str instanceof Date) {
107-
return '"' + str.toISOString() + '"'
108-
}
109-
if (str instanceof RegExp) {
110-
str = str.source
111-
} else {
112-
str = str.toString()
113-
}
114-
}
115-
116102
if (str.length < 42) {
117103
return this.asStringSmall(str)
118104
} else if (STR_ESCAPE.test(str) === false) {

0 commit comments

Comments
 (0)