From d7ced18e06134159b42b8832ba975170359d1364 Mon Sep 17 00:00:00 2001 From: jpsc Date: Mon, 12 Feb 2024 10:09:35 +0100 Subject: [PATCH 1/2] perf(runtime): improve getType gc and speed --- packages/runtime-core/src/componentProps.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index d1822a1638d..8e9e333b441 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -597,8 +597,23 @@ function validatePropName(key: string) { // use function string name to check type constructors // so that it works across vms / iframes. function getType(ctor: Prop): string { - const match = ctor && ctor.toString().match(/^\s*(function|class) (\w+)/) - return match ? match[2] : ctor === null ? 'null' : '' + // Early return for null to avoid unnecessary computations + if (ctor === null) { + return "null"; + } + + // Avoid using regex for common cases by checking the type directly + if (typeof ctor === 'function') { + // Using name property to avoid converting function to string + return ctor.name || ''; + } else if (typeof ctor === 'object') { + // Attempting to directly access constructor name if possible + const name = ctor.constructor && ctor.constructor.name; + return name || ''; + } + + // Fallback for other types (though they're less likely to have meaningful names here) + return ''; } function isSameType(a: Prop, b: Prop): boolean { From d001f7c5c6162bccc909c1b418c41c1a0712ad2e Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:14:29 +0000 Subject: [PATCH 2/2] [autofix.ci] apply automated fixes --- packages/runtime-core/src/componentProps.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 8e9e333b441..2d91affe082 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -599,21 +599,21 @@ function validatePropName(key: string) { function getType(ctor: Prop): string { // Early return for null to avoid unnecessary computations if (ctor === null) { - return "null"; + return 'null' } - + // Avoid using regex for common cases by checking the type directly if (typeof ctor === 'function') { // Using name property to avoid converting function to string - return ctor.name || ''; + return ctor.name || '' } else if (typeof ctor === 'object') { // Attempting to directly access constructor name if possible - const name = ctor.constructor && ctor.constructor.name; - return name || ''; + const name = ctor.constructor && ctor.constructor.name + return name || '' } // Fallback for other types (though they're less likely to have meaningful names here) - return ''; + return '' } function isSameType(a: Prop, b: Prop): boolean {