@@ -140,12 +140,12 @@ function buildObject (schema, code, name) {
140
140
var laterCode = ''
141
141
142
142
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.
146
145
code += `
147
146
if (obj.${ key } !== undefined) {
148
- json += '${ $asString ( key ) } :'`
147
+ json += '${ $asString ( key ) } :'
148
+ `
149
149
150
150
const result = nested ( laterCode , name , '.' + key , schema . properties [ key ] )
151
151
@@ -154,20 +154,20 @@ function buildObject (schema, code, name) {
154
154
155
155
if ( i < a . length - 1 ) {
156
156
code += `
157
- json += \',\'`
157
+ json += \',\'
158
+ `
158
159
}
159
160
160
161
if ( schema . properties [ key ] . required ) {
161
162
code += `
162
163
} else {
163
164
throw new Error('${ key } is required!')
164
- }
165
- `
166
- } else {
167
- code += `
168
- }
169
165
`
170
166
}
167
+
168
+ code += `
169
+ }
170
+ `
171
171
} )
172
172
173
173
code += `
@@ -222,32 +222,38 @@ function nested (laterCode, name, key, schema) {
222
222
switch ( type ) {
223
223
case 'null' :
224
224
code += `
225
- json += $asNull()`
225
+ json += $asNull()
226
+ `
226
227
break
227
228
case 'string' :
228
229
code += `
229
- json += $asString(obj${ key } )`
230
+ json += $asString(obj${ key } )
231
+ `
230
232
break
231
233
case 'number' :
232
234
case 'integer' :
233
235
code += `
234
- json += $asNumber(obj${ key } )`
236
+ json += $asNumber(obj${ key } )
237
+ `
235
238
break
236
239
case 'boolean' :
237
240
code += `
238
- json += $asBoolean(obj${ key } )`
241
+ json += $asBoolean(obj${ key } )
242
+ `
239
243
break
240
244
case 'object' :
241
245
funcName = ( name + key ) . replace ( / [ - . \[ \] ] / g, '' )
242
246
laterCode = buildObject ( schema , laterCode , funcName )
243
247
code += `
244
- json += ${ funcName } (obj${ key } )`
248
+ json += ${ funcName } (obj${ key } )
249
+ `
245
250
break
246
251
case 'array' :
247
252
funcName = ( name + key ) . replace ( / [ - . \[ \] ] / g, '' )
248
253
laterCode = buildArray ( schema , laterCode , funcName )
249
254
code += `
250
- json += ${ funcName } (obj${ key } )`
255
+ json += ${ funcName } (obj${ key } )
256
+ `
251
257
break
252
258
default :
253
259
throw new Error ( `${ type } unsupported` )
0 commit comments