Skip to content

Commit a13d4fe

Browse files
authored
perf: remove deep copy of the entire sanitized entity config in configToJSONSchema (#11342)
Small performance improvement for types generation and anywhere else `configToJSONSchema` can be used. We did a deep copy of the whole sanitized entity config, which can be expensive. Now, to do the needed mutation of `flattenedFields`, we just create a new reference of `flattenedFields` for that.
1 parent 3ffc268 commit a13d4fe

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

packages/payload/src/utilities/configToJSONSchema.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import type { SanitizedGlobalConfig } from '../globals/config/types.js'
1515
import { MissingEditorProp } from '../errors/MissingEditorProp.js'
1616
import { fieldAffectsData } from '../fields/config/types.js'
1717
import { generateJobsJSONSchemas } from '../queues/config/generateJobsJSONSchemas.js'
18-
import { deepCopyObject } from './deepCopyObject.js'
1918
import { toWords } from './formatLabels.js'
2019
import { getCollectionIDFieldTypes } from './getCollectionIDFieldTypes.js'
2120

@@ -719,7 +718,7 @@ export function fieldsToJSONSchema(
719718
// This function is part of the public API and is exported through payload/utilities
720719
export function entityToJSONSchema(
721720
config: SanitizedConfig,
722-
incomingEntity: SanitizedCollectionConfig | SanitizedGlobalConfig,
721+
entity: SanitizedCollectionConfig | SanitizedGlobalConfig,
723722
interfaceNameDefinitions: Map<string, JSONSchema4>,
724723
defaultIDType: 'number' | 'text',
725724
collectionIDFieldTypes?: { [key: string]: 'number' | 'string' },
@@ -729,25 +728,30 @@ export function entityToJSONSchema(
729728
collectionIDFieldTypes = getCollectionIDFieldTypes({ config, defaultIDType })
730729
}
731730

732-
const entity: SanitizedCollectionConfig | SanitizedGlobalConfig = deepCopyObject(incomingEntity)
733731
const title = entity.typescript?.interface
734732
? entity.typescript.interface
735733
: singular(toWords(entity.slug, true))
736734

735+
let mutableFields = [...entity.flattenedFields]
736+
737737
const idField: FieldAffectingData = { name: 'id', type: defaultIDType as 'text', required: true }
738-
const customIdField = entity.flattenedFields.find(
739-
(field) => field.name === 'id',
740-
) as FieldAffectingData
738+
const customIdField = mutableFields.find((field) => field.name === 'id') as FieldAffectingData
741739

742740
if (customIdField && customIdField.type !== 'group' && customIdField.type !== 'tab') {
743-
customIdField.required = true
741+
mutableFields = mutableFields.map((field) => {
742+
if (field === customIdField) {
743+
return { ...field, required: true }
744+
}
745+
746+
return field
747+
})
744748
} else {
745-
entity.flattenedFields.unshift(idField)
749+
mutableFields.unshift(idField)
746750
}
747751

748752
// mark timestamp fields required
749753
if ('timestamps' in entity && entity.timestamps !== false) {
750-
entity.flattenedFields = entity.flattenedFields.map((field) => {
754+
mutableFields = mutableFields.map((field) => {
751755
if (field.name === 'createdAt' || field.name === 'updatedAt') {
752756
return {
753757
...field,
@@ -765,7 +769,7 @@ export function entityToJSONSchema(
765769
(typeof entity.auth?.disableLocalStrategy === 'object' &&
766770
entity.auth.disableLocalStrategy.enableFields))
767771
) {
768-
entity.flattenedFields.push({
772+
mutableFields.push({
769773
name: 'password',
770774
type: 'text',
771775
})
@@ -777,7 +781,7 @@ export function entityToJSONSchema(
777781
title,
778782
...fieldsToJSONSchema(
779783
collectionIDFieldTypes,
780-
entity.flattenedFields,
784+
mutableFields,
781785
interfaceNameDefinitions,
782786
config,
783787
i18n,

0 commit comments

Comments
 (0)