perf(runtime-dom): cache modifier wrapper functions

close #8882
This commit is contained in:
Evan You 2023-11-30 19:00:00 +08:00
parent 4936d2e11a
commit da4a4fb5e8
1 changed files with 50 additions and 38 deletions

View File

@ -32,14 +32,20 @@ const modifierGuards: Record<
/** /**
* @private * @private
*/ */
export const withModifiers = (fn: Function, modifiers: string[]) => { export const withModifiers = (
return (event: Event, ...args: unknown[]) => { fn: Function & { _withMods?: Function },
modifiers: string[]
) => {
return (
fn._withMods ||
(fn._withMods = (event: Event, ...args: unknown[]) => {
for (let i = 0; i < modifiers.length; i++) { for (let i = 0; i < modifiers.length; i++) {
const guard = modifierGuards[modifiers[i]] const guard = modifierGuards[modifiers[i]]
if (guard && guard(event, modifiers)) return if (guard && guard(event, modifiers)) return
} }
return fn(event, ...args) return fn(event, ...args)
} })
)
} }
// Kept for 2.x compat. // Kept for 2.x compat.
@ -57,7 +63,10 @@ const keyNames: Record<string, string | string[]> = {
/** /**
* @private * @private
*/ */
export const withKeys = (fn: Function, modifiers: string[]) => { export const withKeys = (
fn: Function & { _withKeys?: Function },
modifiers: string[]
) => {
let globalKeyCodes: LegacyConfig['keyCodes'] let globalKeyCodes: LegacyConfig['keyCodes']
let instance: ComponentInternalInstance | null = null let instance: ComponentInternalInstance | null = null
if (__COMPAT__) { if (__COMPAT__) {
@ -77,7 +86,9 @@ export const withKeys = (fn: Function, modifiers: string[]) => {
} }
} }
return (event: KeyboardEvent) => { return (
fn._withKeys ||
(fn._withKeys = (event: KeyboardEvent) => {
if (!('key' in event)) { if (!('key' in event)) {
return return
} }
@ -112,5 +123,6 @@ export const withKeys = (fn: Function, modifiers: string[]) => {
} }
} }
} }
} })
)
} }