From 43919dce28d1264d7e8412c0f10871a9dd3f33c0 Mon Sep 17 00:00:00 2001 From: francesco Date: Mon, 22 Apr 2024 11:37:42 +0200 Subject: [PATCH] perf: removed jsonOutput variable because is useless (#711) * jsonOutput is useless Signed-off-by: francesco * cache end of array Signed-off-by: francesco --------- Signed-off-by: francesco --- index.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 9bb50116..f2eabdca 100644 --- a/index.js +++ b/index.js @@ -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)) { @@ -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.\`) @@ -572,9 +571,9 @@ 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 += ',' } }` } @@ -582,17 +581,15 @@ function buildArray (context, location) { 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)