Skip to content

Commit 2f10755

Browse files
authored
perf: improve-perfs (#806)
1 parent ac5035f commit 2f10755

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

index.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ const asInteger = serializer.asInteger.bind(serializer)
3030
3131
`
3232

33-
const validRoundingMethods = [
33+
const validRoundingMethods = new Set([
3434
'floor',
3535
'ceil',
3636
'round',
3737
'trunc'
38-
]
38+
])
3939

40-
const validLargeArrayMechanisms = [
40+
const validLargeArrayMechanisms = new Set([
4141
'default',
4242
'json-stringify'
43-
]
43+
])
4444

4545
let schemaIdCounter = 0
4646

@@ -127,25 +127,28 @@ function build (schema, options) {
127127
}
128128

129129
if (options.rounding) {
130-
if (!validRoundingMethods.includes(options.rounding)) {
130+
if (!validRoundingMethods.has(options.rounding)) {
131131
throw new Error(`Unsupported integer rounding method ${options.rounding}`)
132132
}
133133
}
134134

135135
if (options.largeArrayMechanism) {
136-
if (validLargeArrayMechanisms.includes(options.largeArrayMechanism)) {
136+
if (validLargeArrayMechanisms.has(options.largeArrayMechanism)) {
137137
largeArrayMechanism = options.largeArrayMechanism
138138
} else {
139139
throw new Error(`Unsupported large array mechanism ${options.largeArrayMechanism}`)
140140
}
141141
}
142142

143143
if (options.largeArraySize) {
144-
if (typeof options.largeArraySize === 'string' && Number.isFinite(Number.parseInt(options.largeArraySize, 10))) {
145-
largeArraySize = Number.parseInt(options.largeArraySize, 10)
146-
} else if (typeof options.largeArraySize === 'number' && Number.isInteger(options.largeArraySize)) {
144+
const largeArraySizeType = typeof options.largeArraySize
145+
let parsedNumber
146+
147+
if (largeArraySizeType === 'string' && Number.isFinite((parsedNumber = Number.parseInt(options.largeArraySize, 10)))) {
148+
largeArraySize = parsedNumber
149+
} else if (largeArraySizeType === 'number' && Number.isInteger(options.largeArraySize)) {
147150
largeArraySize = options.largeArraySize
148-
} else if (typeof options.largeArraySize === 'bigint') {
151+
} else if (largeArraySizeType === 'bigint') {
149152
largeArraySize = Number(options.largeArraySize)
150153
} else {
151154
throw new Error(`Unsupported large array size. Expected integer-like, got ${typeof options.largeArraySize} with value ${options.largeArraySize}`)
@@ -348,19 +351,19 @@ function buildInnerObject (context, location) {
348351
const requiredProperties = schema.required || []
349352

350353
// Should serialize required properties first
351-
const propertiesKeys = Object.keys(schema.properties || {}).sort(
354+
const propertiesKeys = new Set(Object.keys(schema.properties || {}).sort(
352355
(key1, key2) => {
353356
const required1 = requiredProperties.includes(key1)
354357
const required2 = requiredProperties.includes(key2)
355358
return required1 === required2 ? 0 : required1 ? -1 : 1
356359
}
357-
)
360+
))
358361
const hasRequiredProperties = requiredProperties.includes(propertiesKeys[0])
359362

360363
let code = 'let value\n'
361364

362365
for (const key of requiredProperties) {
363-
if (!propertiesKeys.includes(key)) {
366+
if (!propertiesKeys.has(key)) {
364367
const sanitizedKey = JSON.stringify(key)
365368
code += `if (obj[${sanitizedKey}] === undefined) throw new Error('${sanitizedKey.replace(/'/g, '\\\'')} is required!')\n`
366369
}
@@ -423,7 +426,7 @@ function buildInnerObject (context, location) {
423426
}
424427

425428
function mergeLocations (context, mergedSchemaId, mergedLocations) {
426-
for (let i = 0; i < mergedLocations.length; i++) {
429+
for (let i = 0, mergedLocationsLength = mergedLocations.length; i < mergedLocationsLength; i++) {
427430
const location = mergedLocations[i]
428431
const schema = location.schema
429432
if (schema.$ref) {
@@ -575,7 +578,7 @@ function buildArray (context, location) {
575578
`
576579

577580
if (Array.isArray(itemsSchema)) {
578-
for (let i = 0; i < itemsSchema.length; i++) {
581+
for (let i = 0, itemsSchemaLength = itemsSchema.length; i < itemsSchemaLength; i++) {
579582
const item = itemsSchema[i]
580583
functionCode += `value = obj[${i}]`
581584
const tmpRes = buildValue(context, itemsLocation.getPropertyLocation(i), 'value')
@@ -842,7 +845,7 @@ function buildAllOf (context, location, input) {
842845
]
843846

844847
const allOfsLocation = location.getPropertyLocation('allOf')
845-
for (let i = 0; i < allOf.length; i++) {
848+
for (let i = 0, allOfLength = allOf.length; i < allOfLength; i++) {
846849
locations.push(allOfsLocation.getPropertyLocation(i))
847850
}
848851

@@ -867,7 +870,7 @@ function buildOneOf (context, location, input) {
867870

868871
let code = ''
869872

870-
for (let index = 0; index < oneOfs.length; index++) {
873+
for (let index = 0, oneOfsLength = oneOfs.length; index < oneOfsLength; index++) {
871874
const optionLocation = oneOfsLocation.getPropertyLocation(index)
872875
const optionSchema = optionLocation.schema
873876

0 commit comments

Comments
 (0)