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-core): memoize getType #9739

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 11 additions & 4 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,19 +594,26 @@ function validatePropName(key: string) {
return false
}

const typeMap = new WeakMap<PropConstructor, string>()

// use function string name to check type constructors
// so that it works across vms / iframes.
function getType(ctor: Prop<any>): string {
function getType(ctor: PropConstructor): string {
if (typeMap.has(ctor)) {
return typeMap.get(ctor)!
}
const match = ctor && ctor.toString().match(/^\s*(function|class) (\w+)/)
return match ? match[2] : ctor === null ? 'null' : ''
const type = match ? match[2] : ctor === null ? 'null' : ''
if (match) typeMap.set(ctor, type)
return type
}

function isSameType(a: Prop<any>, b: Prop<any>): boolean {
function isSameType(a: PropConstructor, b: PropConstructor): boolean {
return getType(a) === getType(b)
}

function getTypeIndex(
type: Prop<any>,
type: PropConstructor,
expectedTypes: PropType<any> | void | null | true
): number {
if (isArray(expectedTypes)) {
Expand Down