Skip to content

Conversation

@asamuzaK
Copy link
Contributor

@asamuzaK asamuzaK commented Dec 27, 2025

Summary
Adds an LRU cache to reduce repeated css-tree parsing/validation and improve performance of CSS parsing utilities.

Part of jsdom/jsdom#3985

What this PR does

  • Adds lib/utils/cache.js: simple LRU wrapper using lru-cache (max: 4096) with a null sentinel for null values.
  • Caches results in lib/parsers.js for:
    • isValidPropertyValue (key: isValidPropertyValue_${prop}_${val}) — caches boolean results
    • resolveCalc (key: resolveCalc_${val}) — caches resolved string value
    • parsePropertyValue (key: parsePropertyValue_${prop}_${val}_${caseSensitive}) — caches parsed AST/plain-object results or false when invalid
  • Adds lru-cache to package.json and package-lock.json.

Benchmarks
cssstyle main:

> jsdom@27.4.0 benchmark
> node ./benchmark/runner --suites style

# style/createElement + setAttribute('style') #
jsdom  x 44.33 ops/sec ±1.27% (59 runs sampled)

# style/createElement + style properties #
jsdom  x 142 ops/sec ±3.34% (72 runs sampled)

# style/createElement + style.cssText #
jsdom  x 36.25 ops/sec ±2.77% (59 runs sampled)

# style/innerHTML: divs with inline styles #
jsdom  x 41.36 ops/sec ±2.46% (55 runs sampled)

# style/innerHTML: simple divs (no styles) #
jsdom  x 3,739 ops/sec ±1.71% (88 runs sampled)

cssstyle PR:

> jsdom@27.4.0 benchmark
> node ./benchmark/runner --suites style

# style/createElement + setAttribute('style') #
jsdom  x 178 ops/sec ±1.46% (83 runs sampled)

# style/createElement + style properties #
jsdom  x 248 ops/sec ±3.14% (78 runs sampled)

# style/createElement + style.cssText #
jsdom  x 148 ops/sec ±3.85% (76 runs sampled)

# style/innerHTML: divs with inline styles #
jsdom  x 154 ops/sec ±2.61% (79 runs sampled)

# style/innerHTML: simple divs (no styles) #
jsdom  x 3,837 ops/sec ±2.53% (89 runs sampled)

Introduced a new cache utility in lib/utils/cache.js using lru-cache to store up to 4096 items, with support for caching null values. Added lru-cache as a dependency in package.json.
Introduces caching for isValidPropertyValue, resolveCalc, and parsePropertyValue to improve performance by avoiding redundant computations. Utilizes getCache and setCache from utils/cache to store and retrieve results based on input parameters.
Rearranged the order of exported functions in module.exports for better organization and consistency. No functional changes were made.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant