Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit cb8f072

Browse files
committed
perf(inject-css.ts*): Memory efficiency up
1 parent 04ada48 commit cb8f072

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

src/_internal/utils/inject-css.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ function createStyleElement(hash: string): HTMLStyleElement | null {
1616
return styleSheets[hash];
1717
}
1818

19-
export function injectCSS(hash: string, sheet: string, string: string) {
20-
if (isInDevelopment) console.log('💫 ' + string + ' executing ...' + sheet);
19+
export function injectCSS(hash: string, sheet: string, context: string) {
20+
if (isInDevelopment) console.log('💫 ' + context + ' executing ...' + sheet);
2121
if (isServer) return;
22-
23-
styleCleanUp();
22+
queueMicrotask(() => {
23+
styleCleanUp();
24+
});
2425
hashCache[hash] = hash;
26+
2527
const styleElement = createStyleElement(hash);
2628
if (styleElement == null) return;
2729

@@ -31,7 +33,7 @@ export function injectCSS(hash: string, sheet: string, string: string) {
3133
function styleCleanUp() {
3234
requestAnimationFrame(() => {
3335
for (const hash in hashCache) {
34-
const classElements = document.getElementsByClassName(hash);
36+
const classElements = document.querySelectorAll(`[class*="${hash}"]`);
3537
if (classElements.length === 0) {
3638
removeStyleElement(hashCache[hash]);
3739
}

src/core/method/create.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@ export function create<T extends ClassesObjectType>(object: ExactClassesObjectTy
1313
get: function (target, prop: string) {
1414
if (typeof prop === 'string' && prop in target) {
1515
const className = prop + '_' + base62Hash;
16-
const classRuleRegex = new RegExp(`\\n\\.${className}[^{]*\\{[^}]*\\}`, 'g');
17-
const mediaBlockRegex = new RegExp(
18-
`\\n@media[^{]+\\{(?:[^{}]*\\{[^{}]*\\})*[^{}]*\\.${className}[^{}]*\\{[^{}]*\\}(?:[^{}]*\\{[^{}]*\\})*[^{}]*\\}`,
19-
'g'
20-
);
21-
const sheet = (Array.from(styleSheet.match(classRuleRegex) || []) as string[])
22-
.concat(Array.from(styleSheet.match(mediaBlockRegex) || []) as string[])
23-
.join('');
24-
if (isInDevelopment) injectCSS(className, sheet, 'sheet');
25-
16+
if (isInDevelopment) injectCSS(base62Hash, styleSheet, 'create');
2617
return isInDevelopment ? className : styles[className];
2718
}
2819
},

src/core/method/set.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { createGlobalStyleSheetPromise, globalStyleSheetPromise, resolveGlobalSt
66
export function set(object: CustomCSSProperties): string {
77
const base62Hash = genBase62Hash(object, 5);
88
const { styleSheet } = styleCompiler(object, base62Hash);
9-
const className = '_' + base62Hash;
9+
const classHash = '_' + base62Hash;
1010
if (typeof globalStyleSheetPromise === 'undefined') createGlobalStyleSheetPromise();
1111
resolveGlobalStyleSheet(styleSheet);
1212

1313
function returnFunction() {
14-
if (isInDevelopment) injectCSS(className, styleSheet, 'style');
15-
return isInDevelopment ? className : styles[className];
14+
if (isInDevelopment) injectCSS(base62Hash, styleSheet, 'set');
15+
return isInDevelopment ? classHash : styles[classHash];
1616
}
1717

1818
return returnFunction() as unknown as string;

0 commit comments

Comments
 (0)