@@ -113,11 +113,10 @@ module.exports = class Serializer {
113113 }
114114 }
115115
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 ) {
120117 return this . asStringSmall ( str )
118+ } else if ( STR_ESCAPE . test ( str ) === false ) {
119+ return '"' + str + '"'
121120 } else {
122121 return JSON . stringify ( str )
123122 }
@@ -130,32 +129,32 @@ module.exports = class Serializer {
130129 // 34 and 92 happens all the time, so we
131130 // have a fast case for them
132131 asStringSmall ( str ) {
133- const l = str . length
132+ const len = str . length
134133 let result = ''
135- let last = 0
136- let found = false
137- let surrogateFound = false
134+ let last = - 1
138135 let point = 255
136+
139137 // eslint-disable-next-line
140- for ( var i = 0 ; i < l && point >= 32 ; i ++ ) {
138+ for ( var i = 0 ; i < len ; i ++ ) {
141139 point = str . charCodeAt ( i )
140+ if ( point < 32 ) {
141+ return JSON . stringify ( str )
142+ }
142143 if ( point >= 0xD800 && point <= 0xDFFF ) {
143144 // The current character is a surrogate.
144- surrogateFound = true
145+ return JSON . stringify ( str )
145146 }
146- if ( point === 34 || point === 92 ) {
147+ if (
148+ point === 0x22 || // '"'
149+ point === 0x5c // '\'
150+ ) {
151+ last === - 1 && ( last = 0 )
147152 result += str . slice ( last , i ) + '\\'
148153 last = i
149- found = true
150154 }
151155 }
152156
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 ) + '"' )
159158 }
160159
161160 getState ( ) {
0 commit comments