Skip to content

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 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.

Cache benchmark
CacheRelative to the fastest of its groupCalls/sCompared
Style recipe, with cache15.7M—
Style recipe, without cache3.24M4.9× slower
Slot style recipe, with cache15.3M—
Slot style recipe, without cache1.44M10.6× slower

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.

Themed recipe benchmark
Themed recipeRelative to the fastest of its groupCalls/sCompared
Style recipe15.9M1.1× faster
Themed style recipe, one theme15.1M—
Themed style recipe, two themes14.6Mon par
Style recipe, without cache3.49M4.3× slower
  • 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.