@@ -140,12 +140,12 @@ function buildObject (schema, code, name) {
140140 var laterCode = ''
141141
142142 Object . keys ( schema . properties ) . forEach ( ( key , i , a ) => {
143- /* code += `
144- if (obj.hasOwnProperty('${key}')) {
145- json += '${$asString(key)}:'`*/
143+ // Using obj.key !== undefined instead of obj.hasOwnProperty(prop) for perf reasons,
144+ // see https://github.com/mcollina/fast-json-stringify/pull/3 for discussion.
146145 code += `
147146 if (obj.${ key } !== undefined) {
148- json += '${ $asString ( key ) } :'`
147+ json += '${ $asString ( key ) } :'
148+ `
149149
150150 const result = nested ( laterCode , name , '.' + key , schema . properties [ key ] )
151151
@@ -154,20 +154,20 @@ function buildObject (schema, code, name) {
154154
155155 if ( i < a . length - 1 ) {
156156 code += `
157- json += \',\'`
157+ json += \',\'
158+ `
158159 }
159160
160161 if ( schema . properties [ key ] . required ) {
161162 code += `
162163 } else {
163164 throw new Error('${ key } is required!')
164- }
165- `
166- } else {
167- code += `
168- }
169165 `
170166 }
167+
168+ code += `
169+ }
170+ `
171171 } )
172172
173173 code += `
@@ -222,32 +222,38 @@ function nested (laterCode, name, key, schema) {
222222 switch ( type ) {
223223 case 'null' :
224224 code += `
225- json += $asNull()`
225+ json += $asNull()
226+ `
226227 break
227228 case 'string' :
228229 code += `
229- json += $asString(obj${ key } )`
230+ json += $asString(obj${ key } )
231+ `
230232 break
231233 case 'number' :
232234 case 'integer' :
233235 code += `
234- json += $asNumber(obj${ key } )`
236+ json += $asNumber(obj${ key } )
237+ `
235238 break
236239 case 'boolean' :
237240 code += `
238- json += $asBoolean(obj${ key } )`
241+ json += $asBoolean(obj${ key } )
242+ `
239243 break
240244 case 'object' :
241245 funcName = ( name + key ) . replace ( / [ - . \[ \] ] / g, '' )
242246 laterCode = buildObject ( schema , laterCode , funcName )
243247 code += `
244- json += ${ funcName } (obj${ key } )`
248+ json += ${ funcName } (obj${ key } )
249+ `
245250 break
246251 case 'array' :
247252 funcName = ( name + key ) . replace ( / [ - . \[ \] ] / g, '' )
248253 laterCode = buildArray ( schema , laterCode , funcName )
249254 code += `
250- json += ${ funcName } (obj${ key } )`
255+ json += ${ funcName } (obj${ key } )
256+ `
251257 break
252258 default :
253259 throw new Error ( `${ type } unsupported` )
0 commit comments