Skip to content

Commit ca39e0d

Browse files
committed
chore: quick perf improve
1 parent ac5035f commit ca39e0d

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

index.js

Lines changed: 18 additions & 14 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,29 @@ 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+
if (largeArraySizeType === 'string') {
146+
const newLargeArraySize = Number.parseInt(options.largeArraySize, 10)
147+
if (Number.isFinite(newLargeArraySize)) {
148+
largeArraySize = newLargeArraySize
149+
}
150+
} else if (largeArraySizeType === 'number' && Number.isInteger(options.largeArraySize)) {
147151
largeArraySize = options.largeArraySize
148-
} else if (typeof options.largeArraySize === 'bigint') {
152+
} else if (largeArraySizeType === 'bigint') {
149153
largeArraySize = Number(options.largeArraySize)
150154
} else {
151155
throw new Error(`Unsupported large array size. Expected integer-like, got ${typeof options.largeArraySize} with value ${options.largeArraySize}`)
@@ -423,7 +427,7 @@ function buildInnerObject (context, location) {
423427
}
424428

425429
function mergeLocations (context, mergedSchemaId, mergedLocations) {
426-
for (let i = 0; i < mergedLocations.length; i++) {
430+
for (let i = 0, mergedLocationsLength = mergedLocations.length; i < mergedLocationsLength; i++) {
427431
const location = mergedLocations[i]
428432
const schema = location.schema
429433
if (schema.$ref) {
@@ -575,7 +579,7 @@ function buildArray (context, location) {
575579
`
576580

577581
if (Array.isArray(itemsSchema)) {
578-
for (let i = 0; i < itemsSchema.length; i++) {
582+
for (let i = 0, itemsSchemaLength = itemsSchema.length; i < itemsSchemaLength; i++) {
579583
const item = itemsSchema[i]
580584
functionCode += `value = obj[${i}]`
581585
const tmpRes = buildValue(context, itemsLocation.getPropertyLocation(i), 'value')
@@ -842,7 +846,7 @@ function buildAllOf (context, location, input) {
842846
]
843847

844848
const allOfsLocation = location.getPropertyLocation('allOf')
845-
for (let i = 0; i < allOf.length; i++) {
849+
for (let i = 0, allOfsLength = allOfsLocation.length; i < allOfsLength; i++) {
846850
locations.push(allOfsLocation.getPropertyLocation(i))
847851
}
848852

@@ -867,7 +871,7 @@ function buildOneOf (context, location, input) {
867871

868872
let code = ''
869873

870-
for (let index = 0; index < oneOfs.length; index++) {
874+
for (let index = 0, oneOfsLength = oneOfs.length; index < oneOfsLength; index++) {
871875
const optionLocation = oneOfsLocation.getPropertyLocation(index)
872876
const optionSchema = optionLocation.schema
873877

0 commit comments

Comments
 (0)