Skip to content

Commit

Permalink
perf: removed jsonOutput variable because is useless (#711)
Browse files Browse the repository at this point in the history
* jsonOutput is useless

Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>

* cache end of array

Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>

---------

Signed-off-by: francesco <francesco.bagnoli.69@gmail.com>
  • Loading branch information
cesco69 committed Apr 22, 2024
1 parent ed47c66 commit 43919dc
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,12 +540,13 @@ function buildArray (context, location) {
}

if (largeArrayMechanism === 'json-stringify') {
functionCode += `if (arrayLength && arrayLength >= ${largeArraySize}) return JSON.stringify(obj)\n`
functionCode += `if (arrayLength >= ${largeArraySize}) return JSON.stringify(obj)\n`
}

functionCode += `
const arrayEnd = arrayLength - 1
let value
let jsonOutput = ''
let json = ''
`

if (Array.isArray(itemsSchema)) {
Expand All @@ -556,11 +557,9 @@ function buildArray (context, location) {
functionCode += `
if (${i} < arrayLength) {
if (${buildArrayTypeCondition(item.type, `[${i}]`)}) {
let json = ''
${tmpRes}
jsonOutput += json
if (${i} < arrayLength - 1) {
jsonOutput += ','
if (${i} < arrayEnd) {
json += ','
}
} else {
throw new Error(\`Item at ${i} does not match schema definition.\`)
Expand All @@ -572,27 +571,25 @@ function buildArray (context, location) {
if (schema.additionalItems) {
functionCode += `
for (let i = ${itemsSchema.length}; i < arrayLength; i++) {
jsonOutput += JSON.stringify(obj[i])
if (i < arrayLength - 1) {
jsonOutput += ','
json += JSON.stringify(obj[i])
if (i < arrayEnd) {
json += ','
}
}`
}
} else {
const code = buildValue(context, itemsLocation, 'obj[i]')
functionCode += `
for (let i = 0; i < arrayLength; i++) {
let json = ''
${code}
jsonOutput += json
if (i < arrayLength - 1) {
jsonOutput += ','
if (i < arrayEnd) {
json += ','
}
}`
}

functionCode += `
return \`[\${jsonOutput}]\`
return \`[\${json}]\`
}`

context.functions.push(functionCode)
Expand Down

0 comments on commit 43919dc

Please sign in to comment.