Primer React parses color schemes on runtime using a reduce on a pretty big object:
| constcolorSchemes: Record<Scheme,SchemeValue>=Object.entries(colors).reduce( |
| (acc,[name,variables])=>{ |
| const{colors, shadows}=partitionColors(variables) |
| return{ |
| ...acc, |
| [name]: { |
| colors: omitScale(colors), |
| shadows: omitScale(shadows), |
| }, |
| } |
| }, |
| {}asRecord<Scheme,SchemeValue>, |
| ) |
This is run whenever Primer is loaded in a JS bundle, which means that clients will have to run this expensive calculation before even starting to render anything.
Primer could pre-compile colorSchemes and include it in the bundle instead of having clients calculating it :)
I wrote two scripts comparing load times between the two methods here https://gist.github.com/manuelpuyol/f0b3c140c579e195374b30f25d5da833
In my tests, the load times went from ~15ms to <1ms!
Primer React parses color schemes on runtime using a reduce on a pretty big object:
react/packages/react/src/theme.ts
Lines 64 to 76 in 32b0cf7
This is run whenever Primer is loaded in a JS bundle, which means that clients will have to run this expensive calculation before even starting to render anything.
Primer could pre-compile
colorSchemesand include it in the bundle instead of having clients calculating it :)I wrote two scripts comparing load times between the two methods here https://gist.github.com/manuelpuyol/f0b3c140c579e195374b30f25d5da833
In my tests, the load times went from ~15ms to <1ms!