Skip to content

Commit 603a1e1

Browse files
authoredFeb 13, 2024··
perf(runtime): improve getType() GC and speed (#10327)
1 parent 3f92126 commit 603a1e1

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed
 

‎packages/runtime-core/src/componentProps.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,23 @@ function validatePropName(key: string) {
597597
// use function string name to check type constructors
598598
// so that it works across vms / iframes.
599599
function getType(ctor: Prop<any>): string {
600-
const match = ctor && ctor.toString().match(/^\s*(function|class) (\w+)/)
601-
return match ? match[2] : ctor === null ? 'null' : ''
600+
// Early return for null to avoid unnecessary computations
601+
if (ctor === null) {
602+
return 'null'
603+
}
604+
605+
// Avoid using regex for common cases by checking the type directly
606+
if (typeof ctor === 'function') {
607+
// Using name property to avoid converting function to string
608+
return ctor.name || ''
609+
} else if (typeof ctor === 'object') {
610+
// Attempting to directly access constructor name if possible
611+
const name = ctor.constructor && ctor.constructor.name
612+
return name || ''
613+
}
614+
615+
// Fallback for other types (though they're less likely to have meaningful names here)
616+
return ''
602617
}
603618

604619
function isSameType(a: Prop<any>, b: Prop<any>): boolean {

0 commit comments

Comments
 (0)
Please sign in to comment.