native-recipe performance
- 5×as fast with the cache, for a style recipe
- 11×as fast with the cache, for a slot style recipe
- 95%of the speed of a style recipe, for a themed one
What makes a style cheap for React Native is its identity, not
StyleSheet.create, which returns the styles it is given and only checks
their types. A recipe returns the same frozen object for the same
variants, so the style prop keeps its identity between renders, and
caches it under one integer per selection, so most calls are a lookup (see
How it works).
The cache
Section titled “The cache”The benchmarks call a style recipe, and a slot style recipe, which builds a style for each slot, with five selections in turn, which cover default variants, compound variants, and boolean variants. Each compares the recipe with the same recipe built on every call.
Themed recipes
Section titled “Themed recipes”A themed recipe finds the recipe of its theme, then calls it, still
without allocations. It keeps the recipe of the last theme apart, so
calling it again with the same theme skips the lookup; another theme is
found in a WeakMap. The benchmark calls a themed recipe with the five
selections, twice in each iteration: with one theme both times, or with a
light theme, then a dark one. It compares it with the same recipe without
a theme, and with the recipe of each theme built on every call.
Method
Section titled “Method”- The benchmarks first check that each recipe returns the expected styles, for each theme.
- They import the package by its name, so they run the built bundle, not the sources.
- The benchmarks assert that a style recipe, a slot style recipe, and a themed recipe are faster with their cache than without it.
Run them with pnpm bench, which builds the package first, or with
pnpm measure, which also saves the results shown here.