Skip to content

Commit ef9bd2d

Browse files
committed
refactor(shared): use Map and Set to replace object
1 parent 1ecaac3 commit ef9bd2d

File tree

8 files changed

+98
-76
lines changed

8 files changed

+98
-76
lines changed

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

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,24 @@ const modifierGuards: Record<
5252
export const withModifiers = <
5353
T extends (event: Event, ...args: unknown[]) => any,
5454
>(
55-
fn: T & { _withMods?: { [key: string]: T } },
55+
fn: T & { _withMods?: Map<string, T> },
5656
modifiers: VOnModifiers[],
5757
): T => {
58-
const cache = fn._withMods || (fn._withMods = {})
58+
const cache = fn._withMods || (fn._withMods = new Map())
5959
const cacheKey = modifiers.join('.')
60-
return (
61-
cache[cacheKey] ||
62-
(cache[cacheKey] = ((event, ...args) => {
63-
for (let i = 0; i < modifiers.length; i++) {
64-
const guard = modifierGuards[modifiers[i] as ModifierGuards]
65-
if (guard && guard(event, modifiers)) return
66-
}
67-
return fn(event, ...args)
68-
}) as T)
69-
)
60+
const cached = cache.get(cacheKey)
61+
if (cached) {
62+
return cached
63+
}
64+
const modifier = ((event, ...args) => {
65+
for (let i = 0; i < modifiers.length; i++) {
66+
const guard = modifierGuards[modifiers[i] as ModifierGuards]
67+
if (guard && guard(event, modifiers)) return
68+
}
69+
return fn(event, ...args)
70+
}) as T
71+
cache.set(cacheKey, modifier)
72+
return modifier
7073
}
7174

7275
// Kept for 2.x compat.
@@ -88,7 +91,7 @@ const keyNames: Record<
8891
* @private
8992
*/
9093
export const withKeys = <T extends (event: KeyboardEvent) => any>(
91-
fn: T & { _withKeys?: { [k: string]: T } },
94+
fn: T & { _withKeys?: Map<string, T> },
9295
modifiers: string[],
9396
): T => {
9497
let globalKeyCodes: LegacyConfig['keyCodes']
@@ -110,54 +113,60 @@ export const withKeys = <T extends (event: KeyboardEvent) => any>(
110113
}
111114
}
112115

113-
const cache: { [k: string]: T } = fn._withKeys || (fn._withKeys = {})
116+
const cache: Map<string, T> = fn._withKeys || (fn._withKeys = new Map())
114117
const cacheKey = modifiers.join('.')
115118

116-
return (
117-
cache[cacheKey] ||
118-
(cache[cacheKey] = (event => {
119-
if (!('key' in event)) {
120-
return
121-
}
119+
const cached = cache.get(cacheKey)
120+
if (cached) {
121+
return cached
122+
}
122123

123-
const eventKey = hyphenate(event.key)
124+
const withKey = (event => {
125+
if (!('key' in event)) {
126+
return
127+
}
128+
129+
const eventKey = hyphenate(event.key)
130+
if (
131+
modifiers.some(
132+
k =>
133+
k === eventKey ||
134+
keyNames[k as unknown as CompatModifiers] === eventKey,
135+
)
136+
) {
137+
return fn(event)
138+
}
139+
140+
if (__COMPAT__) {
141+
const keyCode = String(event.keyCode)
124142
if (
125-
modifiers.some(
126-
k =>
127-
k === eventKey ||
128-
keyNames[k as unknown as CompatModifiers] === eventKey,
129-
)
143+
compatUtils.isCompatEnabled(
144+
DeprecationTypes.V_ON_KEYCODE_MODIFIER,
145+
instance,
146+
) &&
147+
modifiers.some(mod => mod == keyCode)
130148
) {
131149
return fn(event)
132150
}
133-
134-
if (__COMPAT__) {
135-
const keyCode = String(event.keyCode)
136-
if (
137-
compatUtils.isCompatEnabled(
138-
DeprecationTypes.V_ON_KEYCODE_MODIFIER,
139-
instance,
140-
) &&
141-
modifiers.some(mod => mod == keyCode)
142-
) {
143-
return fn(event)
144-
}
145-
if (globalKeyCodes) {
146-
for (const mod of modifiers) {
147-
const codes = globalKeyCodes[mod]
148-
if (codes) {
149-
const matches = isArray(codes)
150-
? codes.some(code => String(code) === keyCode)
151-
: String(codes) === keyCode
152-
if (matches) {
153-
return fn(event)
154-
}
151+
if (globalKeyCodes) {
152+
for (const mod of modifiers) {
153+
const codes = globalKeyCodes[mod]
154+
if (codes) {
155+
const matches = isArray(codes)
156+
? codes.some(code => String(code) === keyCode)
157+
: String(codes) === keyCode
158+
if (matches) {
159+
return fn(event)
155160
}
156161
}
157162
}
158163
}
159-
}) as T)
160-
)
164+
}
165+
}) as T
166+
167+
cache.set(cacheKey, withKey)
168+
169+
return withKey
161170
}
162171

163172
export type VOnDirective = Directive<any, any, VOnModifiers>

packages/runtime-dom/src/modules/style.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,24 @@ function setStyle(
103103
}
104104

105105
const prefixes = ['Webkit', 'Moz', 'ms']
106-
const prefixCache: Record<string, string> = {}
106+
const prefixCache: Map<string, string> = new Map()
107107

108108
function autoPrefix(style: CSSStyleDeclaration, rawName: string): string {
109-
const cached = prefixCache[rawName]
109+
const cached = prefixCache.get(rawName)
110110
if (cached) {
111111
return cached
112112
}
113113
let name = camelize(rawName)
114114
if (name !== 'filter' && name in style) {
115-
return (prefixCache[rawName] = name)
115+
prefixCache.set(rawName, name)
116+
return name
116117
}
117118
name = capitalize(name)
118119
for (let i = 0; i < prefixes.length; i++) {
119120
const prefixed = prefixes[i] + name
120121
if (prefixed in style) {
121-
return (prefixCache[rawName] = prefixed)
122+
prefixCache.set(rawName, prefixed)
123+
return prefixed
122124
}
123125
}
124126
return rawName

packages/server-renderer/src/helpers/ssrCompile.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type SSRRenderFunction = (
1717
parentInstance: ComponentInternalInstance,
1818
) => void
1919

20-
const compileCache: Record<string, SSRRenderFunction> = Object.create(null)
20+
const compileCache: Map<string, SSRRenderFunction> = new Map()
2121

2222
export function ssrCompile(
2323
template: string,
@@ -62,7 +62,7 @@ export function ssrCompile(
6262
},
6363
)
6464

65-
const cached = compileCache[cacheKey]
65+
const cached = compileCache.get(cacheKey)
6666
if (cached) {
6767
return cached
6868
}
@@ -89,5 +89,8 @@ export function ssrCompile(
8989
'vue/server-renderer': helpers,
9090
}
9191
const fakeRequire = (id: 'vue' | 'vue/server-renderer') => requireMap[id]
92-
return (compileCache[cacheKey] = Function('require', code)(fakeRequire))
92+
93+
const ssrRenderFunction = Function('require', code)(fakeRequire)
94+
compileCache.set(cacheKey, ssrRenderFunction)
95+
return ssrRenderFunction
9396
}

packages/shared/src/domAttrConfig.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,18 @@ const unsafeAttrCharRE = /[>/="'\u0009\u000a\u000c\u0020]/
3737
const attrValidationCache: Map<string, boolean> = new Map()
3838

3939
export function isSSRSafeAttrName(name: string): boolean {
40-
if (!attrValidationCache.has(name)) {
41-
const isUnsafe = unsafeAttrCharRE.test(name)
42-
if (isUnsafe) {
43-
console.error(`unsafe attribute name: ${name}`)
44-
}
45-
attrValidationCache.set(name, !isUnsafe)
40+
const cached = attrValidationCache.get(name)
41+
if (cached) {
42+
return cached
4643
}
4744

48-
return attrValidationCache.get(name)!
45+
const isUnsafe = unsafeAttrCharRE.test(name)
46+
if (isUnsafe) {
47+
console.error(`unsafe attribute name: ${name}`)
48+
}
49+
attrValidationCache.set(name, !isUnsafe)
50+
51+
return !isUnsafe
4952
}
5053

5154
export const propsToAttrMap: Record<string, string | undefined> = {

packages/shared/src/general.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ export const isBuiltInDirective: (key: string) => boolean =
9393
const cacheStringFunction = <T extends (str: string) => string>(fn: T): T => {
9494
const cache: Map<string, string> = new Map()
9595
return ((str: string) => {
96-
if (!cache.has(str)) {
97-
cache.set(str, fn(str))
96+
const cached = cache.get(str)
97+
if (cached) {
98+
return cached
9899
}
99-
return cache.get(str)!
100+
const res = fn(str)
101+
cache.set(str, res)
102+
return res
100103
}) as T
101104
}
102105

packages/shared/src/makeMap.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
/*! #__NO_SIDE_EFFECTS__ */
1010
export function makeMap(str: string): (key: string) => boolean {
11-
const map = Object.create(null)
12-
for (const key of str.split(',')) map[key] = 1
13-
return val => val in map
11+
const map = new Set()
12+
for (const key of str.split(',')) map.add(key)
13+
return val => map.has(val)
1414
}

packages/vue-compat/src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
warnDeprecation,
2727
} from '../../runtime-core/src/compat/compatConfig'
2828

29-
const compileCache: Record<string, RenderFunction> = Object.create(null)
29+
const compileCache: Map<string, RenderFunction> = new Map()
3030

3131
function compileToFunction(
3232
template: string | HTMLElement,
@@ -42,7 +42,7 @@ function compileToFunction(
4242
}
4343

4444
const key = genCacheKey(template, options)
45-
const cached = compileCache[key]
45+
const cached = compileCache.get(key)
4646
if (cached) {
4747
return cached
4848
}
@@ -101,7 +101,8 @@ function compileToFunction(
101101
// mark the function as runtime compiled
102102
;(render as InternalRenderFunction)._rc = true
103103

104-
return (compileCache[key] = render)
104+
compileCache.set(key, render)
105+
return render
105106
}
106107

107108
registerRuntimeCompiler(compileToFunction)

packages/vue/src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if (__DEV__) {
2525
initDev()
2626
}
2727

28-
const compileCache: Record<string, RenderFunction> = Object.create(null)
28+
const compileCache: Map<string, RenderFunction> = new Map()
2929

3030
function compileToFunction(
3131
template: string | HTMLElement,
@@ -41,7 +41,7 @@ function compileToFunction(
4141
}
4242

4343
const key = genCacheKey(template, options)
44-
const cached = compileCache[key]
44+
const cached = compileCache.get(key)
4545
if (cached) {
4646
return cached
4747
}
@@ -98,7 +98,8 @@ function compileToFunction(
9898
// mark the function as runtime compiled
9999
;(render as InternalRenderFunction)._rc = true
100100

101-
return (compileCache[key] = render)
101+
compileCache.set(key, render)
102+
return render
102103
}
103104

104105
registerRuntimeCompiler(compileToFunction)

0 commit comments

Comments
 (0)