From 431a8e476dc9aeb5cf66e2f394a8bd12973c9b92 Mon Sep 17 00:00:00 2001 From: francesco Date: Fri, 19 Apr 2024 12:38:15 +0200 Subject: [PATCH 1/2] jsonOutput is useless Signed-off-by: francesco --- index.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 9bb50116..d4495998 100644 --- a/index.js +++ b/index.js @@ -545,7 +545,7 @@ function buildArray (context, location) { functionCode += ` let value - let jsonOutput = '' + let json = '' ` if (Array.isArray(itemsSchema)) { @@ -556,11 +556,9 @@ function buildArray (context, location) { functionCode += ` if (${i} < arrayLength) { if (${buildArrayTypeCondition(item.type, `[${i}]`)}) { - let json = '' ${tmpRes} - jsonOutput += json if (${i} < arrayLength - 1) { - jsonOutput += ',' + json += ',' } } else { throw new Error(\`Item at ${i} does not match schema definition.\`) @@ -572,9 +570,9 @@ function buildArray (context, location) { if (schema.additionalItems) { functionCode += ` for (let i = ${itemsSchema.length}; i < arrayLength; i++) { - jsonOutput += JSON.stringify(obj[i]) + json += JSON.stringify(obj[i]) if (i < arrayLength - 1) { - jsonOutput += ',' + json += ',' } }` } @@ -582,17 +580,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 += ',' + json += ',' } }` } functionCode += ` - return \`[\${jsonOutput}]\` + return \`[\${json}]\` }` context.functions.push(functionCode) From 446e87bfcb22934027938600f3b4a621f33f535d Mon Sep 17 00:00:00 2001 From: francesco Date: Fri, 19 Apr 2024 14:02:04 +0200 Subject: [PATCH 2/2] cache end of array Signed-off-by: francesco --- index.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index d4495998..f2eabdca 100644 --- a/index.js +++ b/index.js @@ -540,10 +540,11 @@ 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 json = '' ` @@ -557,7 +558,7 @@ function buildArray (context, location) { if (${i} < arrayLength) { if (${buildArrayTypeCondition(item.type, `[${i}]`)}) { ${tmpRes} - if (${i} < arrayLength - 1) { + if (${i} < arrayEnd) { json += ',' } } else { @@ -571,7 +572,7 @@ function buildArray (context, location) { functionCode += ` for (let i = ${itemsSchema.length}; i < arrayLength; i++) { json += JSON.stringify(obj[i]) - if (i < arrayLength - 1) { + if (i < arrayEnd) { json += ',' } }` @@ -581,7 +582,7 @@ function buildArray (context, location) { functionCode += ` for (let i = 0; i < arrayLength; i++) { ${code} - if (i < arrayLength - 1) { + if (i < arrayEnd) { json += ',' } }`