Skip to content

Commit 6a2d615

Browse files
authored
perf: optimize hash class generation and selector injection logic (#241)
1 parent e964b75 commit 6a2d615

File tree

4 files changed

+6
-9
lines changed

4 files changed

+6
-9
lines changed

src/StyleContext.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ export function createCache() {
2323
const { firstChild } = document.head;
2424

2525
Array.from(styles).forEach((style) => {
26-
(style as any)[CSS_IN_JS_INSTANCE] =
27-
(style as any)[CSS_IN_JS_INSTANCE] || cssinjsInstanceId;
26+
(style as any)[CSS_IN_JS_INSTANCE] ||= cssinjsInstanceId;
2827

2928
// Not force move if no head
3029
if ((style as any)[CSS_IN_JS_INSTANCE] === cssinjsInstanceId) {

src/hooks/useCacheToken.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export default function useCacheToken<
190190
// Optimize for `useStyleRegister` performance
191191
const mergedSalt = `${salt}_${cssVar.prefix}`;
192192
const hashId = hash(mergedSalt);
193-
const hashCls = `${hashPrefix}-${hash(mergedSalt)}`;
193+
const hashCls = `${hashPrefix}-${hashId}`;
194194
actualToken._tokenKey = token2key(actualToken, mergedSalt);
195195

196196
// Replace token value with css variables

src/hooks/useGlobalCache.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export default function useGlobalCache<CacheType>(
8080
onCacheEffect?.(cacheContent);
8181
effectMap.set(fullPathStr, true);
8282

83-
// 微任务清理混存,可以认为是单次 batch render 中只触发一次 effect
83+
// 微任务清理缓存,可以认为是单次 batch render 中只触发一次 effect
8484
Promise.resolve().then(() => {
8585
effectMap.delete(fullPathStr);
8686
});

src/hooks/useStyleRegister.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import StyleContext, {
1515
ATTR_MARK,
1616
CSS_IN_JS_INSTANCE,
1717
} from '../StyleContext';
18-
import { isClientSide, toStyleStr } from '../util';
18+
import { isClientSide, toStyleStr, where } from '../util';
1919
import {
2020
CSS_FILE_STYLE,
2121
existPath,
@@ -100,15 +100,13 @@ function isCompoundCSSProperty(value: CSSObject[string]) {
100100
function injectSelectorHash(
101101
key: string,
102102
hashId: string,
103-
hashPriority?: HashPriority,
103+
hashPriority: HashPriority = 'high',
104104
) {
105105
if (!hashId) {
106106
return key;
107107
}
108108

109-
const hashClassName = `.${hashId}`;
110-
const hashSelector =
111-
hashPriority === 'low' ? `:where(${hashClassName})` : hashClassName;
109+
const hashSelector = where({ hashCls: hashId, hashPriority });
112110

113111
// 注入 hashId
114112
const keys = key.split(',').map((k) => {

0 commit comments

Comments
 (0)