Skip to content

Commit 0f99c88

Browse files
authored
perf(serializer): avoid addComma when not necessary
1 parent a08113e commit 0f99c88

2 files changed

Lines changed: 70 additions & 9 deletions

File tree

‎index.js‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,14 @@ function buildInnerObject (context, location) {
372372
code += 'let json = JSON_STR_BEGIN_OBJECT\n'
373373

374374
let addComma = ''
375-
if (!hasRequiredProperties) {
375+
const needComma = !hasRequiredProperties && (propertiesKeys.size > 1 || (schema.patternProperties || schema.additionalProperties !== false))
376+
if (needComma) {
376377
code += 'let addComma = false\n'
377378
addComma = '!addComma && (addComma = true) || (json += JSON_STR_COMMA)'
378379
}
379380

380381
let counterValue = 0
382+
let i = 0
381383
for (const key of propertiesKeys) {
382384
let propertyLocation = propertiesLocation.getPropertyLocation(key)
383385
if (propertyLocation.schema.$ref) {
@@ -399,7 +401,7 @@ function buildInnerObject (context, location) {
399401

400402
if (defaultValue !== undefined) {
401403
code += ` else {
402-
${addComma}
404+
${i > 0 ? addComma : (needComma ? 'addComma = true' : '')}
403405
json += ${JSON.stringify(sanitizedKey + ':' + JSON.stringify(defaultValue))}
404406
}
405407
`
@@ -415,6 +417,8 @@ function buildInnerObject (context, location) {
415417
if (hasRequiredProperties) {
416418
addComma = 'json += \',\''
417419
}
420+
421+
i++
418422
}
419423

420424
if (schema.patternProperties || schema.additionalProperties) {
@@ -587,10 +591,10 @@ function buildArray (context, location) {
587591
functionCode += `
588592
if (${i} < arrayLength) {
589593
if (${buildArrayTypeCondition(item.type, value)}) {
590-
${tmpRes}
591-
if (${i} < arrayEnd) {
594+
if (${i}) {
592595
json += JSON_STR_COMMA
593596
}
597+
${tmpRes}
594598
} else {
595599
throw new Error(\`Item at ${i} does not match schema definition.\`)
596600
}
@@ -601,21 +605,21 @@ function buildArray (context, location) {
601605
if (schema.additionalItems) {
602606
functionCode += `
603607
for (let i = ${itemsSchema.length}; i < arrayLength; i++) {
604-
json += JSON.stringify(obj[i])
605-
if (i < arrayEnd) {
608+
if (i) {
606609
json += JSON_STR_COMMA
607610
}
611+
json += JSON.stringify(obj[i])
608612
}`
609613
}
610614
} else {
611615
const code = buildValue(context, itemsLocation, 'value')
612616
functionCode += `
613617
for (let i = 0; i < arrayLength; i++) {
614-
const value = obj[i]
615-
${code}
616-
if (i < arrayEnd) {
618+
if (i) {
617619
json += JSON_STR_COMMA
618620
}
621+
const value = obj[i]
622+
${code}
619623
}`
620624
}
621625

‎test/json-add-comma.test.js‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict'
2+
3+
const { test } = require('node:test')
4+
const build = require('..')
5+
6+
test('additionalProperties: false', (t) => {
7+
t.plan(1)
8+
const stringify = build({
9+
title: 'additionalProperties',
10+
type: 'object',
11+
properties: {
12+
foo: {
13+
type: 'string'
14+
}
15+
},
16+
additionalProperties: false
17+
})
18+
19+
const obj = { foo: 'a', bar: 'b', baz: 'c' }
20+
t.assert.equal(stringify(obj), '{"foo":"a"}')
21+
})
22+
23+
test('additionalProperties: {}', (t) => {
24+
t.plan(1)
25+
const stringify = build({
26+
title: 'additionalProperties',
27+
type: 'object',
28+
properties: {
29+
foo: {
30+
type: 'string'
31+
}
32+
},
33+
additionalProperties: {}
34+
})
35+
36+
const obj = { foo: 'a', bar: 'b', baz: 'c' }
37+
t.assert.equal(stringify(obj), '{"foo":"a","bar":"b","baz":"c"}')
38+
})
39+
40+
test('additionalProperties: {type: string}', (t) => {
41+
t.plan(1)
42+
const stringify = build({
43+
title: 'additionalProperties',
44+
type: 'object',
45+
properties: {
46+
foo: {
47+
type: 'string'
48+
}
49+
},
50+
additionalProperties: {
51+
type: 'string'
52+
}
53+
})
54+
55+
const obj = { foo: 'a', bar: 'b', baz: 'c' }
56+
t.assert.equal(stringify(obj), '{"foo":"a","bar":"b","baz":"c"}')
57+
})

0 commit comments

Comments
 (0)