Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dive into React Performance

wuct
October 23, 2016

Dive into React Performance

Three ways to improve the performance of a React app:

1. Reduce call to React.createElement
2. Avoid reconciliation
3. Avoid re-mounting

This talk explains why and how do we use these approaches.

wuct

October 23, 2016
Tweet

More Decks by wuct

Other Decks in Programming

Transcript

  1. createElement(View, {}, [ createElement(View), createElement(View) ]) <View> <View /> <View

    /> </View> { type: View, props: { children: [ { type: View }, { type: View }, ] } }
  2. render() { return ( <table> <thead> <tr> <th>Header 1</th> <th>Header

    2</th> </tr> </thead> <tfoot> <tr> <td>Footer 1</td> <td>Footer 2</td> </tr> </tfoot> <tbody> { // ... } </tbody> </table> ) } const headerAndFooter = <thead> <tr> <th>Header 1</th> <th>Header 2</th> </tr> </thead> <tfoot> <tr> <td>Footer 1</td> <td>Footer 2</td> </tr> </tfoot> render() { return ( <table> {headerAndFooter} <tbody> { // ... } </tbody> </table> ) }
  3. Pure Render Optimization shoudComponentUpdate(nextProps, nextState) { return ( !shallowEqual(nextProps, this.props)

    || !shallowEqual(nextState, this.state) ) } class extends React.PureComponent { // ... } or
  4. Anti Pure Render render() { return ( <PureComponent inlineFunc={() =>

    {}} funcBind={func.bind(this)} arrayLiteral={[]} newArray={map(…)} objectLiteral={{}} newObject={assign({}, …)} > <NestedChild /> </PureComponent> ) }
  5. React Internal Bailout • Bailout when prevElement === nextElement •

    Don’t bailout when prevContext !== nextContext • React always create a new context • Don’t use setState() and getChildContext() in one component
  6. class extends React.Component { getChildContext() { return this.state } componentDidMount()

    { setInterval(() => this.setState({ time: this.state.time + 1 }) , 1000) } // ... }
  7. render() { return isDarkTheme ? <DarkApp /> : <LightApp />

    } render() { <App dark={isDarkTheme} /> }
  8. render () { return ( <List> { data.map(item => <Item

    key={item.uniqueId} /> ) } </List> ) } render () { return ( <List> { data.map(item => <Item key={Math.random()} /> ) } </List> ) }
  9. React Perf Tool • Currently, only supports the development version

    of React • Perf.start() • Perf.stop() • Perf.printWasted() • Perf.printOperations()
  10. class Child extends React.Component { render() { return <div />

    } } class App extends React.Component { state = { title: 'Foo' } render() { return ( <div> {this.state.title} <Child /> </div> ) } }
  11. class App extends React.Component { state = { theme: 'light'

    } render() { return ( <div> { this.state.theme === 'light' ? <Light /> : <Dark /> } </div> ) } }
  12. class App extends React.Component { state = { theme: 'light'

    } render() { return ( <div> <Content theme={this.state.theme} /> </div> ) } }
  13. React Fiber • Is still experimental • Prioritization • Return

    multiple elements from render • Try it now: npm i [email protected] import ReactDOMFiber from 'react-dom/fiber'