From 41e5b4e4b69372e55a4928a4be82fdfb58346cfb Mon Sep 17 00:00:00 2001 From: Balint Gabor <127662+gbalint@users.noreply.github.com> Date: Wed, 4 Sep 2024 10:47:21 +0200 Subject: [PATCH] Minor optimizations in getComponentDescriptorForTarget (#6306) **Problem:** 1. Let's early return for intrinsic elements, which are not component instances, so no reason to search for component descriptors at all. 2. Let's remove the regex replaces which remove file extensions and replace them with `stripExtension` function calls, which are significantly faster. 3. **Manual Tests:** I hereby swear that: - [x] I opened a hydrogen project and it loaded - [x] I could navigate to various routes in Preview mode --- .../property-controls/property-controls-utils.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/editor/src/core/property-controls/property-controls-utils.ts b/editor/src/core/property-controls/property-controls-utils.ts index 9b7e63590f07..168dfabb6d68 100644 --- a/editor/src/core/property-controls/property-controls-utils.ts +++ b/editor/src/core/property-controls/property-controls-utils.ts @@ -27,6 +27,7 @@ import { getFilePathMappings } from '../model/project-file-utils' import { valueDependentCache } from '../shared/memoize' import * as EP from '../shared/element-path' import { shallowEqual } from '../shared/equality-utils' +import { stripExtension } from '../../components/custom-code/custom-code-utils' export function propertyControlsForComponentInFile( componentName: string, @@ -170,6 +171,10 @@ function getComponentDescriptorForTargetInner( underlyingFilePath: string, ) => { if (isJSXElement(element)) { + if (isIntrinsicElement(element.name)) { + return null + } + const importedFrom = importedFromWhere( filePathMappings, underlyingFilePath, @@ -180,11 +185,7 @@ function getComponentDescriptorForTargetInner( let filenameForLookup: string | null = null if (importedFrom == null) { - if (isIntrinsicElement(element.name)) { - return null - } else { - filenameForLookup = underlyingFilePath.replace(/\.(js|jsx|ts|tsx)$/, '') - } + filenameForLookup = stripExtension(underlyingFilePath) } else { filenameForLookup = importedFrom.filePath } @@ -216,7 +217,7 @@ function getComponentDescriptorForTargetInner( ) const trimmedPath = absolutePath.includes('/') - ? absolutePath.replace(/\.(js|jsx|ts|tsx)$/, '') + ? stripExtension(absolutePath) : absolutePath return propertyControlsInfo[trimmedPath]?.[nameAsString]