File tree Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ function build (schema, options) {
1515 `
1616 code += `
1717 ${ $asString . toString ( ) }
18+ ${ $asStringSmall . toString ( ) }
1819 ${ $asNumber . toString ( ) }
1920 ${ $asNull . toString ( ) }
2021 ${ $asBoolean . toString ( ) }
@@ -83,7 +84,36 @@ function $asString (str) {
8384 str = str . toString ( )
8485 }
8586
86- return JSON . stringify ( str )
87+ if ( str . length < 42 ) {
88+ return $asStringSmall ( str )
89+ } else {
90+ return JSON . stringify ( str )
91+ }
92+ }
93+
94+ // magically escape strings for json
95+ // relying on their charCodeAt
96+ // everything below 32 needs JSON.stringify()
97+ // 34 and 92 happens all the time, so we
98+ // have a fast case for them
99+ function $asStringSmall ( str ) {
100+ var result = ''
101+ var last = 0
102+ var l = str . length
103+ var point = 255
104+ for ( var i = 0 ; i < l && point >= 32 ; i ++ ) {
105+ point = str . charCodeAt ( i )
106+ if ( point === 34 || point === 92 ) {
107+ result += str . slice ( last , i ) + '\\' + str [ i ]
108+ last = i + 1
109+ }
110+ }
111+ if ( last === 0 ) {
112+ result = str
113+ } else {
114+ result += str . slice ( last )
115+ }
116+ return point < 32 ? JSON . stringify ( str ) : '"' + result + '"'
87117}
88118
89119function addPatternProperties ( schema , externalSchema ) {
You can’t perform that action at this time.
0 commit comments