@@ -113,11 +113,10 @@ module.exports = class Serializer {
113
113
}
114
114
}
115
115
116
- // Fast escape chars check
117
- if ( ! STR_ESCAPE . test ( str ) ) {
118
- return '"' + str + '"'
119
- } else if ( str . length < 42 ) {
116
+ if ( str . length < 42 ) {
120
117
return this . asStringSmall ( str )
118
+ } else if ( STR_ESCAPE . test ( str ) === false ) {
119
+ return '"' + str + '"'
121
120
} else {
122
121
return JSON . stringify ( str )
123
122
}
@@ -130,32 +129,32 @@ module.exports = class Serializer {
130
129
// 34 and 92 happens all the time, so we
131
130
// have a fast case for them
132
131
asStringSmall ( str ) {
133
- const l = str . length
132
+ const len = str . length
134
133
let result = ''
135
- let last = 0
136
- let found = false
137
- let surrogateFound = false
134
+ let last = - 1
138
135
let point = 255
136
+
139
137
// eslint-disable-next-line
140
- for ( var i = 0 ; i < l && point >= 32 ; i ++ ) {
138
+ for ( var i = 0 ; i < len ; i ++ ) {
141
139
point = str . charCodeAt ( i )
140
+ if ( point < 32 ) {
141
+ return JSON . stringify ( str )
142
+ }
142
143
if ( point >= 0xD800 && point <= 0xDFFF ) {
143
144
// The current character is a surrogate.
144
- surrogateFound = true
145
+ return JSON . stringify ( str )
145
146
}
146
- if ( point === 34 || point === 92 ) {
147
+ if (
148
+ point === 0x22 || // '"'
149
+ point === 0x5c // '\'
150
+ ) {
151
+ last === - 1 && ( last = 0 )
147
152
result += str . slice ( last , i ) + '\\'
148
153
last = i
149
- found = true
150
154
}
151
155
}
152
156
153
- if ( ! found ) {
154
- result = str
155
- } else {
156
- result += str . slice ( last )
157
- }
158
- return ( ( point < 32 ) || ( surrogateFound === true ) ) ? JSON . stringify ( str ) : '"' + result + '"'
157
+ return ( last === - 1 && ( '"' + str + '"' ) ) || ( '"' + result + str . slice ( last ) + '"' )
159
158
}
160
159
161
160
getState ( ) {
0 commit comments