Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance: Cache obj[key] access into #707

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ function buildInnerObject (context, location) {
)
const hasRequiredProperties = requiredProperties.includes(propertiesKeys[0])

let code = ''
let code = 'let value\n'

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

code += `
if (obj[${sanitizedKey}] !== undefined) {
value = obj[${sanitizedKey}]
if (value !== undefined) {
${addComma}
json += ${JSON.stringify(sanitizedKey + ':')}
${buildValue(context, propertyLocation, `obj[${sanitizedKey}]`)}
${buildValue(context, propertyLocation, 'value')}
}`

if (defaultValue !== undefined) {
Expand Down Expand Up @@ -543,13 +544,15 @@ function buildArray (context, location) {
}

functionCode += `
let value
let jsonOutput = ''
`

if (Array.isArray(itemsSchema)) {
for (let i = 0; i < itemsSchema.length; i++) {
const item = itemsSchema[i]
const tmpRes = buildValue(context, itemsLocation.getPropertyLocation(i), `obj[${i}]`)
functionCode += `value = obj[${i}]`
const tmpRes = buildValue(context, itemsLocation.getPropertyLocation(i), 'value')
functionCode += `
if (${i} < arrayLength) {
if (${buildArrayTypeCondition(item.type, `[${i}]`)}) {
Expand Down Expand Up @@ -600,33 +603,33 @@ function buildArrayTypeCondition (type, accessor) {
let condition
switch (type) {
case 'null':
condition = `obj${accessor} === null`
condition = 'value === null'
break
case 'string':
condition = `typeof obj${accessor} === 'string' ||
obj${accessor} === null ||
obj${accessor} instanceof Date ||
obj${accessor} instanceof RegExp ||
condition = `typeof value === 'string' ||
value === null ||
value instanceof Date ||
value instanceof RegExp ||
(
typeof obj${accessor} === "object" &&
typeof obj${accessor}.toString === "function" &&
obj${accessor}.toString !== Object.prototype.toString
typeof value === "object" &&
typeof value.toString === "function" &&
value.toString !== Object.prototype.toString
)`
break
case 'integer':
condition = `Number.isInteger(obj${accessor})`
condition = 'Number.isInteger(value)'
break
case 'number':
condition = `Number.isFinite(obj${accessor})`
condition = 'Number.isFinite(value)'
break
case 'boolean':
condition = `typeof obj${accessor} === 'boolean'`
condition = 'typeof value === \'boolean\''
break
case 'object':
condition = `obj${accessor} && typeof obj${accessor} === 'object' && obj${accessor}.constructor === Object`
condition = 'value && typeof value === \'object\' && value.constructor === Object'
break
case 'array':
condition = `Array.isArray(obj${accessor})`
condition = 'Array.isArray(value)'
break
default:
if (Array.isArray(type)) {
Expand Down