Need for speed - Optimizing React

We talk about JavaScript. Each month in Warsaw, Poland.

Speaker

Kamil Grabek

"Need for speed. Optimizing React"

2017-06-14

kamil.grabek@gmail.com

Who am I?

Front-end developer

Bluesoft, Warsaw

Tech freak, sci-fi lover, astro maniac.

"Kiedy życie zaskoczy,
pytam co ma dla mnie jeszcze"

NEED FOR SPEED
OPTIMIZING REACT

IS REACT FAST OUT OF THE BOX?

UNOPTIMIZED APPLICATION

UNOPTIMIZED APPLICATION

WHAT IS OUR GOAL OPTIMIZATION

WHEN START OPTIMIZATION

DIAGNOSTICS

DIAGNOSTICS

USING CONSOLE IN RENDER METHOD

            
              class MyComponent extends React.Component {
                ...
                render() {
                  console.log(`Rendering ${this.constructor.name}`);
                  return (
                    < div>...< /div>
                  );
                }
                ...
              }
            
        

WHY-DID-YOU-UPDATE

https://github.com/garbles/why-did-you-update

REACT PERFORMANCE TOOLS

WASTED OPERATIONS

TIME SPENT PROCESSING COMPONENTS

DOM OPERATIONS PERFORMED

https://facebook.github.io/react/docs/perf.html

?REACT_PERF + DEV TOOLS TIMELINE

`

COMPOSITION

COMPOSITION

JSX AND COMPONENTS

MAKING TEMPLATES WRONG WAY

                    
                      < div>
                      {invitations.map(invitation => (
                        < div
                          key={invitation.id}
                          < div>{invitation.number}< /div>
                          ...
                        />
                      ))}
                      < /div>
                  
                

MAKING TEMPLATES GOOD WAY

                
                  < div>
                  {invitations.map(invitation => (
                    < Invitation
                      key={invitation.id}
                      {...getInvitationProps(invitation)}
                    />
                  ))}
                  < /div>
                
              

LIFECYCLE - UPDATING

  • componentWillReceiveProps
  • shouldComponentUpdate
  • componentWillUpdate
  • render
  • componentDidUpdate

shouldComponentUpdate

            
              class MyComponent extends React.Component {
                ...
                shouldComponentUpdate(nextPros, nextState) {
                  const hasPropsChanged = shallowEqual(nextProps, this.props);
                  const hasStateChanged = shallowEqual(nextState, this.state);
                  return !hasPropsChanged && !hasStateChanged;
                }
                ...
              }
            
        

APPLICATION WITH SCU

APPLICATION WITH SCU

HOW NOT TO USE IT

IMMUTABILITY

MUTABILITY
VS
IMMUTABILITY

MUTABLE STRUCTURES AND REACT

WHY IMMUTABLE STRUCTURES

immutability-helper

            
                import update from 'immutability-helper';
                const newData = update(myData, {
                  x: { y: {z: { $set: 7 } } },
                  a: { b: { $push: [9] } }
                });
            
        

immutability-helper - commands

https://www.npmjs.com/package/immutability-helper

MEMOIZATION

MEMOIZATION

WHAT IS MEMOIZATION?

MOIZE

https://www.npmjs.com/package/moize

        
            import moize from 'moize';
            const method = (a, b) => {
              return a + b;
            };
            const memoized = moize(method);
            memoized(2, 4); // 6
            memoized(2, 4); // 6, pulled from cache
        
      

MOIZE AND REACT

            
                import moize from 'moize';
                const Foo = ({bar, baz}) => {
                  return (
                    < div>
                      {bar} {baz}
                    < /div>
                  );
                };
                export default moize.react(Foo);
            
          

APPLICATION WITH SCU AND MEMOIZATION

APPLICATION WITH SCU AND MEMOIZATION

WHAT MORE?

FINAL THOUGHTS

RESOURCES

THANKS FOR
LISTENING. QUESTIONS?