Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion packages/css-processor/src/CSSProcessor.ts
Comment thread
pataar marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@ export type MixedStyleDeclaration = Omit<
[k in MixedSizeCSSPropertiesKeys]?: number | string;
};

// Bounded to avoid unbounded growth on long-lived processors.
const INLINE_CSS_CACHE_LIMIT = 256;
Comment thread
pataar marked this conversation as resolved.
Outdated

export class CSSProcessor {
public readonly registry: CSSPropertiesValidationRegistry;
// LRU cache: same inline string compiles to the same CSSProcessedProps.
private inlineCssCache: Map<string, CSSProcessedProps> = new Map();
Comment thread
pataar marked this conversation as resolved.
Outdated
constructor(userConfig?: Partial<CSSProcessorConfig>) {
const config = {
...defaultCSSProcessorConfig,
Expand All @@ -126,7 +131,24 @@ export class CSSProcessor {
}

compileInlineCSS(inlineCSS: string): CSSProcessedProps {
const cache = this.inlineCssCache;
const cached = cache.get(inlineCSS);
if (cached !== undefined) {
// LRU touch: re-insert to mark as most recently used.
cache.delete(inlineCSS);
cache.set(inlineCSS, cached);
return cached;
}
const parseRun = new CSSInlineParseRun(inlineCSS, this.registry);
return parseRun.exec();
const result = parseRun.exec();
if (cache.size >= INLINE_CSS_CACHE_LIMIT) {
// Evict oldest (Map preserves insertion order).
const oldest = cache.keys().next().value;
if (oldest !== undefined) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this check protect us from? undefined is fine as a map key

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2026-05-11 at 22 00 37

TypeScript says no 😁

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could force it, but the check is cheap i guess

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, let's keep it that way, I just checked the behaviour in a JS environment and it sticked out

cache.delete(oldest);
}
}
cache.set(inlineCSS, result);
return result;
}
}