'React Native inline styles and performance

Does the following:

<Text style={{color: 'blue', fontSize: 30}} />

Have any performance implications compared to:

<Text style={styles.blueButton} />

...

const styles = StyleSheet.create({
  blueButton: {
    color: 'blue',
    fontSize: 30,
  }
});


Solution 1:[1]

From the docs for StyleSheet

Performance:

  • Making a stylesheet from a style object makes it possible to refer to it by ID instead of creating a new style object every time.
  • It also allows to send the style only once through the bridge. All subsequent uses are going to refer an id (not implemented yet).

Another benefit is that style errors will be generated at compile time as opposed to run time.

I personally still like using inline styles (and creating new components for shared styles) because it makes the code more readable for me and the performance hit has not been noticeable.

Solution 2:[2]

As mentioned in the comments of the selected answer, the performance comment has been removed from the RN docs, but in my project (a mobile game using RN that uses multiple active Animated components), using Stylesheet instead of inline styles helps performance.

My main view where around 5~10 Animated components are active, the UI FPS drops to 15~18 when I use inline styles, but if I use Stylesheet it has a 60fps with little to no stagger. Note that I had to implement this in most components especially the Animated components and its child components in order to see the improvements, I still have some inline styles lying around in other components.

When it comes to performance, your UI FPS is affected by your js activity. So reducing/optimizing JS load will help to keep your FPS perform better. In my project, some style values are computed, but they only need to be computed during the initial load. When I use inline styles, these values are computed every re-render, so using stylesheets makes more sense since there will be no recomputing. This is especially important when handling image assets.

Bottom line, use Stylesheet when you can. It will help in the long run and avoid any unnecessary updates from inline to stylesheet.

Solution 3:[3]

Yes it will help performance slightly. That's not really what matters, though.

There are many ways to improve performance such that the increase in performance is so slight that no human would ever notice it while using your app, in which case there's no real benefit to said performance improvement.

So the question is really whether or not it improves performance enough to where it's noticeable. The answer to that is "almost definitely not", unless you're constantly rendering hundreds/thousands of components each with inline styles at the same time (which should never happen).

Inline styles plus style utility components are what I prefer because they're faster to write and they're more maintainable (easier to make changes to the correct style because you can find the style for the component you're wanting to change more easily). Plus you don't have to arbitrarily give things names which is a lot of sunk time/effort.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 FuzzyTree
Solution 2
Solution 3 ICW