Skip to content

Commit 686e6ce

Browse files
authored
Cache obj access (#707)
Cache into a variable the object[key] value Signed-off-by: francesco <[email protected]>
1 parent e87268d commit 686e6ce

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

index.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ function buildInnerObject (context, location) {
333333
)
334334
const hasRequiredProperties = requiredProperties.includes(propertiesKeys[0])
335335

336-
let code = ''
336+
let code = 'let value\n'
337337

338338
for (const key of requiredProperties) {
339339
if (!propertiesKeys.includes(key)) {
@@ -360,10 +360,11 @@ function buildInnerObject (context, location) {
360360
const isRequired = requiredProperties.includes(key)
361361

362362
code += `
363-
if (obj[${sanitizedKey}] !== undefined) {
363+
value = obj[${sanitizedKey}]
364+
if (value !== undefined) {
364365
${addComma}
365366
json += ${JSON.stringify(sanitizedKey + ':')}
366-
${buildValue(context, propertyLocation, `obj[${sanitizedKey}]`)}
367+
${buildValue(context, propertyLocation, 'value')}
367368
}`
368369

369370
if (defaultValue !== undefined) {
@@ -543,13 +544,15 @@ function buildArray (context, location) {
543544
}
544545

545546
functionCode += `
547+
let value
546548
let jsonOutput = ''
547549
`
548550

549551
if (Array.isArray(itemsSchema)) {
550552
for (let i = 0; i < itemsSchema.length; i++) {
551553
const item = itemsSchema[i]
552-
const tmpRes = buildValue(context, itemsLocation.getPropertyLocation(i), `obj[${i}]`)
554+
functionCode += `value = obj[${i}]`
555+
const tmpRes = buildValue(context, itemsLocation.getPropertyLocation(i), 'value')
553556
functionCode += `
554557
if (${i} < arrayLength) {
555558
if (${buildArrayTypeCondition(item.type, `[${i}]`)}) {
@@ -600,33 +603,33 @@ function buildArrayTypeCondition (type, accessor) {
600603
let condition
601604
switch (type) {
602605
case 'null':
603-
condition = `obj${accessor} === null`
606+
condition = 'value === null'
604607
break
605608
case 'string':
606-
condition = `typeof obj${accessor} === 'string' ||
607-
obj${accessor} === null ||
608-
obj${accessor} instanceof Date ||
609-
obj${accessor} instanceof RegExp ||
609+
condition = `typeof value === 'string' ||
610+
value === null ||
611+
value instanceof Date ||
612+
value instanceof RegExp ||
610613
(
611-
typeof obj${accessor} === "object" &&
612-
typeof obj${accessor}.toString === "function" &&
613-
obj${accessor}.toString !== Object.prototype.toString
614+
typeof value === "object" &&
615+
typeof value.toString === "function" &&
616+
value.toString !== Object.prototype.toString
614617
)`
615618
break
616619
case 'integer':
617-
condition = `Number.isInteger(obj${accessor})`
620+
condition = 'Number.isInteger(value)'
618621
break
619622
case 'number':
620-
condition = `Number.isFinite(obj${accessor})`
623+
condition = 'Number.isFinite(value)'
621624
break
622625
case 'boolean':
623-
condition = `typeof obj${accessor} === 'boolean'`
626+
condition = 'typeof value === \'boolean\''
624627
break
625628
case 'object':
626-
condition = `obj${accessor} && typeof obj${accessor} === 'object' && obj${accessor}.constructor === Object`
629+
condition = 'value && typeof value === \'object\' && value.constructor === Object'
627630
break
628631
case 'array':
629-
condition = `Array.isArray(obj${accessor})`
632+
condition = 'Array.isArray(value)'
630633
break
631634
default:
632635
if (Array.isArray(type)) {

0 commit comments

Comments
 (0)