Skip to content

Instantly share code, notes, and snippets.

@jlebensold
Created June 15, 2017 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlebensold/00b44e00a89bd4c684db6e0a3d943862 to your computer and use it in GitHub Desktop.
Save jlebensold/00b44e00a89bd4c684db6e0a3d943862 to your computer and use it in GitHub Desktop.
Why do inline styles outperform Stylesheet.create() ?
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
steps = 20000;
const IS_INLINE = 0;
inlineStyles = () => {
const texts = [];
for (var i = 0; i < steps; i++) {
texts.push(<View key={`tx_${i}`} style={{ borderRadius: 15, padding: 3, backgroundColor: "red", width: 5, height: 5 }}></View> )
}
return texts;
}
stylesheetStyles = () => {
const texts = [];
for (var i = 0; i < steps; i++) {
texts.push(<View key={`tx_${i}`} style={styles.styleContainer}></View>)
}
return texts;
}
export default class managing_styles extends Component {
constructor(props) {
super(props);
if (IS_INLINE) {
this.texts = inlineStyles();
} else {
this.texts = stylesheetStyles();
}
this.t = Date.now()
}
render() {
return (
<View style={styles.container}>
<View style={styles.wrap}>
{ this.texts }
</View>
<Text style={styles.instructions}>
{ Date.now() - this.t }
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
styleContainer: {
height: 5,
borderRadius: 15,
padding: 3,
width: 5,
backgroundColor: "blue",
},
wrap: {
padding: 20,
flex: 1,
flexDirection: "row",
flexWrap: "wrap",
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('managing_styles', () => managing_styles);
@jlebensold
Copy link
Author

The docs state calling Stylesheet.create() should outperform inline declaration: https://facebook.github.io/react-native/docs/stylesheet.html

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