Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(runtime): improve getType() GC and speed #10327

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>): 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<any>, b: Prop<any>): boolean {
Expand Down
Loading