Skip to content

Commit da4a4fb

Browse files
committed
perf(runtime-dom): cache modifier wrapper functions
close #8882
1 parent 4936d2e commit da4a4fb

File tree

1 file changed

+50
-38
lines changed
  • packages/runtime-dom/src/directives

1 file changed

+50
-38
lines changed

packages/runtime-dom/src/directives/vOn.ts

+50-38
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@ const modifierGuards: Record<
3232
/**
3333
* @private
3434
*/
35-
export const withModifiers = (fn: Function, modifiers: string[]) => {
36-
return (event: Event, ...args: unknown[]) => {
37-
for (let i = 0; i < modifiers.length; i++) {
38-
const guard = modifierGuards[modifiers[i]]
39-
if (guard && guard(event, modifiers)) return
40-
}
41-
return fn(event, ...args)
42-
}
35+
export const withModifiers = (
36+
fn: Function & { _withMods?: Function },
37+
modifiers: string[]
38+
) => {
39+
return (
40+
fn._withMods ||
41+
(fn._withMods = (event: Event, ...args: unknown[]) => {
42+
for (let i = 0; i < modifiers.length; i++) {
43+
const guard = modifierGuards[modifiers[i]]
44+
if (guard && guard(event, modifiers)) return
45+
}
46+
return fn(event, ...args)
47+
})
48+
)
4349
}
4450

4551
// Kept for 2.x compat.
@@ -57,7 +63,10 @@ const keyNames: Record<string, string | string[]> = {
5763
/**
5864
* @private
5965
*/
60-
export const withKeys = (fn: Function, modifiers: string[]) => {
66+
export const withKeys = (
67+
fn: Function & { _withKeys?: Function },
68+
modifiers: string[]
69+
) => {
6170
let globalKeyCodes: LegacyConfig['keyCodes']
6271
let instance: ComponentInternalInstance | null = null
6372
if (__COMPAT__) {
@@ -77,40 +86,43 @@ export const withKeys = (fn: Function, modifiers: string[]) => {
7786
}
7887
}
7988

80-
return (event: KeyboardEvent) => {
81-
if (!('key' in event)) {
82-
return
83-
}
84-
85-
const eventKey = hyphenate(event.key)
86-
if (modifiers.some(k => k === eventKey || keyNames[k] === eventKey)) {
87-
return fn(event)
88-
}
89+
return (
90+
fn._withKeys ||
91+
(fn._withKeys = (event: KeyboardEvent) => {
92+
if (!('key' in event)) {
93+
return
94+
}
8995

90-
if (__COMPAT__) {
91-
const keyCode = String(event.keyCode)
92-
if (
93-
compatUtils.isCompatEnabled(
94-
DeprecationTypes.V_ON_KEYCODE_MODIFIER,
95-
instance
96-
) &&
97-
modifiers.some(mod => mod == keyCode)
98-
) {
96+
const eventKey = hyphenate(event.key)
97+
if (modifiers.some(k => k === eventKey || keyNames[k] === eventKey)) {
9998
return fn(event)
10099
}
101-
if (globalKeyCodes) {
102-
for (const mod of modifiers) {
103-
const codes = globalKeyCodes[mod]
104-
if (codes) {
105-
const matches = isArray(codes)
106-
? codes.some(code => String(code) === keyCode)
107-
: String(codes) === keyCode
108-
if (matches) {
109-
return fn(event)
100+
101+
if (__COMPAT__) {
102+
const keyCode = String(event.keyCode)
103+
if (
104+
compatUtils.isCompatEnabled(
105+
DeprecationTypes.V_ON_KEYCODE_MODIFIER,
106+
instance
107+
) &&
108+
modifiers.some(mod => mod == keyCode)
109+
) {
110+
return fn(event)
111+
}
112+
if (globalKeyCodes) {
113+
for (const mod of modifiers) {
114+
const codes = globalKeyCodes[mod]
115+
if (codes) {
116+
const matches = isArray(codes)
117+
? codes.some(code => String(code) === keyCode)
118+
: String(codes) === keyCode
119+
if (matches) {
120+
return fn(event)
121+
}
110122
}
111123
}
112124
}
113125
}
114-
}
115-
}
126+
})
127+
)
116128
}

0 commit comments

Comments
 (0)