File tree Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -214,6 +214,27 @@ const benchmarks = [
214
214
b5 : true
215
215
}
216
216
} ,
217
+ {
218
+ name : 'simple object' ,
219
+ schema : {
220
+ title : 'Example Schema' ,
221
+ type : 'object' ,
222
+ properties : {
223
+ firstName : {
224
+ type : 'string'
225
+ } ,
226
+ lastName : {
227
+ type : [ 'string' , 'null' ]
228
+ } ,
229
+ age : {
230
+ description : 'Age in years' ,
231
+ type : 'integer' ,
232
+ minimum : 0
233
+ }
234
+ }
235
+ } ,
236
+ input : { firstName : 'Max' , lastName : 'Power' , age : 22 }
237
+ } ,
217
238
{
218
239
name : 'object with const string property' ,
219
240
schema : {
Original file line number Diff line number Diff line change @@ -123,15 +123,30 @@ function build (schema, options) {
123
123
const location = new Location ( schema , rootSchemaId )
124
124
const code = buildValue ( location , 'input' )
125
125
126
- const contextFunctionCode = `
126
+ let contextFunctionCode
127
+
128
+ // If we have only the invocation of the 'anonymous0' function, we would
129
+ // basically just wrap the 'anonymous0' function in the 'main' function and
130
+ // and the overhead of the intermediate variabe 'json'. We can avoid the
131
+ // wrapping and the unnecessary memory allocation by aliasing 'anonymous0' to
132
+ // 'main'
133
+ if ( code === 'json += anonymous0(input)' ) {
134
+ contextFunctionCode = `
135
+ ${ contextFunctions . join ( '\n' ) }
136
+ const main = anonymous0
137
+ return main
138
+ `
139
+ } else {
140
+ contextFunctionCode = `
127
141
function main (input) {
128
142
let json = ''
129
143
${ code }
130
144
return json
131
145
}
132
146
${ contextFunctions . join ( '\n' ) }
133
147
return main
134
- `
148
+ `
149
+ }
135
150
136
151
const serializer = new Serializer ( options )
137
152
const validator = new Validator ( options . ajv )
You can’t perform that action at this time.
0 commit comments