You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
processStylexRules is O(rules × consts) when defineConsts is used #1960
processStylexRules substitutes defineConsts values with a nested loop. For every non-const rule and every direction, it iterates over all consts and calls replaceAll twice:
for(const[varRef,constValue]ofconstsMap.entries()){original=original.replaceAll(varRef,replacement);// ...and replaceAll(`${constName}:`, `${targetName}:`) for var() consts}
So the cost grows with rules × consts. With a few hundred consts (for example, a typed token module of var(--…) constants) and a few thousand rules, the CSS build goes from milliseconds to hundreds of milliseconds. In dev, @stylexjs/unplugin calls processStylexRules on every CSS request, and the Vite plugin triggers one whenever a transformed module changes the rule set. That makes dev servers and browser test runs noticeably slower.
This takes 169ms with 0.19.0, and 16ms with the change below. On a real app (6,242 rules, about 200 consts) it takes 313ms before and 12ms after.
Proposed fix: resolve consts once (as today), precompute each const's custom-property rename target, then substitute in one pass per rule string:
constconstTargetNames=newMap();// "--hash" -> "--target" for var() consts// ...constconstRefRegex=/var\(--[A-Za-z0-9_-]+\)|--[A-Za-z0-9_-]+:/g;styleObj[dir]=original.replace(constRefRegex,(match)=>{if(match.startsWith('var(')){constv=constsMap.get(match);returnv==null ? match : String(v);}consttarget=constTargetNames.get(match.slice(0,-1));returntarget==null ? match : `${target}:`;});
The output is byte-identical to the current implementation on the two real rule sets I compared (6,242 and 906 rules, with and without useLayers). I can open a PR.
Activity
henryqdineen commentedon Oct 19, 2026
We are running into similar perf issues and have open PR to hopefully fix in #1700.