Skip to content

colorSchemes calculation slows down initial load #4336

Description

@manuelpuyol

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!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions