From 003a4cfcee10f9e6a611a79b2b719271e0d23339 Mon Sep 17 00:00:00 2001 From: francesco Date: Wed, 8 Oct 2025 11:02:00 +0200 Subject: [PATCH 1/3] perf: monomorfic variables Signed-off-by: francesco --- index.js | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index 1bcb115d..ccfcc31b 100644 --- a/index.js +++ b/index.js @@ -357,7 +357,7 @@ function buildInnerObject (context, location) { ) const hasRequiredProperties = requiredProperties.includes(propertiesKeys[0]) - let code = 'let value\n' + let code = '' for (const key of requiredProperties) { if (!propertiesKeys.includes(key)) { @@ -381,15 +381,16 @@ function buildInnerObject (context, location) { } const sanitizedKey = JSON.stringify(key) + const value = 'value_' + key.replace(/[^a-zA-Z0-9]/g, '_') const defaultValue = propertyLocation.schema.default const isRequired = requiredProperties.includes(key) code += ` - value = obj[${sanitizedKey}] - if (value !== undefined) { + let ${value} = obj[${sanitizedKey}] + if (${value} !== undefined) { ${addComma} json += ${JSON.stringify(sanitizedKey + ':')} - ${buildValue(context, propertyLocation, 'value')} + ${buildValue(context, propertyLocation, `${value}`)} }` if (defaultValue !== undefined) { @@ -570,18 +571,18 @@ function buildArray (context, location) { functionCode += ` const arrayEnd = arrayLength - 1 - let value let json = '' ` if (Array.isArray(itemsSchema)) { for (let i = 0; i < itemsSchema.length; i++) { const item = itemsSchema[i] - functionCode += `value = obj[${i}]` - const tmpRes = buildValue(context, itemsLocation.getPropertyLocation(i), 'value') + const value = `value_${i}` + functionCode += `const ${value} = obj[${i}]` + const tmpRes = buildValue(context, itemsLocation.getPropertyLocation(i), value) functionCode += ` if (${i} < arrayLength) { - if (${buildArrayTypeCondition(item.type, 'value')}) { + if (${buildArrayTypeCondition(item.type, value)}) { ${tmpRes} if (${i} < arrayEnd) { json += JSON_STR_COMMA @@ -596,8 +597,7 @@ function buildArray (context, location) { if (schema.additionalItems) { functionCode += ` for (let i = ${itemsSchema.length}; i < arrayLength; i++) { - value = obj[i] - json += JSON.stringify(value) + json += JSON.stringify(obj[i]) if (i < arrayEnd) { json += JSON_STR_COMMA } @@ -607,7 +607,7 @@ function buildArray (context, location) { const code = buildValue(context, itemsLocation, 'value') functionCode += ` for (let i = 0; i < arrayLength; i++) { - value = obj[i] + const value = obj[i] ${code} if (i < arrayEnd) { json += JSON_STR_COMMA @@ -627,33 +627,33 @@ function buildArrayTypeCondition (type, accessor) { let condition switch (type) { case 'null': - condition = 'value === null' + condition = `${accessor} === null` break case 'string': - condition = `typeof value === 'string' || - value === null || - value instanceof Date || - value instanceof RegExp || + condition = `typeof ${accessor} === 'string' || + ${accessor} === null || + ${accessor} instanceof Date || + ${accessor} instanceof RegExp || ( - typeof value === "object" && - typeof value.toString === "function" && - value.toString !== Object.prototype.toString + typeof ${accessor} === "object" && + typeof ${accessor}.toString === "function" && + ${accessor}.toString !== Object.prototype.toString )` break case 'integer': - condition = 'Number.isInteger(value)' + condition = `Number.isInteger(${accessor})` break case 'number': - condition = 'Number.isFinite(value)' + condition = `Number.isFinite(${accessor})` break case 'boolean': - condition = 'typeof value === \'boolean\'' + condition = `typeof ${accessor} === 'boolean'` break case 'object': - condition = 'value && typeof value === \'object\' && value.constructor === Object' + condition = `${accessor} && typeof ${accessor} === 'object' && ${accessor}.constructor === Object` break case 'array': - condition = 'Array.isArray(value)' + condition = `Array.isArray(${accessor})` break default: if (Array.isArray(type)) { From d433cda2ebcf9f8a6437eabdf42aa70ae5748880 Mon Sep 17 00:00:00 2001 From: francesco Date: Wed, 8 Oct 2025 11:06:19 +0200 Subject: [PATCH 2/3] fix: make const Signed-off-by: francesco --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index ccfcc31b..31d4185f 100644 --- a/index.js +++ b/index.js @@ -386,7 +386,7 @@ function buildInnerObject (context, location) { const isRequired = requiredProperties.includes(key) code += ` - let ${value} = obj[${sanitizedKey}] + const ${value} = obj[${sanitizedKey}] if (${value} !== undefined) { ${addComma} json += ${JSON.stringify(sanitizedKey + ':')} From ca9dbc7295dc46892c2e35ca7fc014e04c521e20 Mon Sep 17 00:00:00 2001 From: francesco Date: Wed, 8 Oct 2025 16:42:08 +0200 Subject: [PATCH 3/3] fix: avoid collision Signed-off-by: francesco --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 31d4185f..35e1bb00 100644 --- a/index.js +++ b/index.js @@ -374,6 +374,7 @@ function buildInnerObject (context, location) { addComma = '!addComma && (addComma = true) || (json += JSON_STR_COMMA)' } + let counterValue = 0 for (const key of propertiesKeys) { let propertyLocation = propertiesLocation.getPropertyLocation(key) if (propertyLocation.schema.$ref) { @@ -381,7 +382,7 @@ function buildInnerObject (context, location) { } const sanitizedKey = JSON.stringify(key) - const value = 'value_' + key.replace(/[^a-zA-Z0-9]/g, '_') + const value = 'value_' + key.replace(/[^a-zA-Z0-9]/g, '_') + '_' + (counterValue++) const defaultValue = propertyLocation.schema.default const isRequired = requiredProperties.includes(key)