React at 60 FPS - Optimizing performance

Speaker

Kamil Grabek

"React at 60 FPS - Optimizing performance"

github.com/G3F4

kamil.grabek@gmail.com

REACT AT 60 FPS OPTIMIZING PERFORMANCE

By Kamil Grabek

IS REACT FAST OUT OF THE BOX?

Unoptimized application

UNOPTIMIZED APPLICATION

RECONCILIATION

WHAT CHANGED?

https://www.toptal.com/react/optimizing-react-performance

WHAT REACT WOULD NORMALLY DO?

https://www.toptal.com/react/optimizing-react-performance

WHAT REACT SHOULD DO?

https://www.toptal.com/react/optimizing-react-performance

OPTIMIZATIONS GOAL

WHEN START OPTIMIZING

DIAGNOSTICS

USING CONSOLE IN RENDER

     class MyComponent extends React.Component {
    render() {
      console.log('Rendering MyComponent');

      return (
        <div>...</div>
      );
    }
 }
  

PROFILING REACT APP

HIGHLIGHT UPDATES

HIGHLIGHT UPDATES

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}
     data={invitation}
     onUpdate={this.handleUpdate}
   />
 ))}
 </div>
              

LIFECYCLE - UPDATING

  • getDerivedStateFromProps
  • shouldComponentUpdate
  • render
  • componentDidUpdate

shouldComponentUpdate

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

React PureComponent and memo

             // class style
 class MyComponent extends React.PureComponent {
   render() {
      ...
   }
 }

 // or with functional components
 const MyComponent = memo(props => (...));
        

BAD PRACTICES

Application with pure components

Application with pure components

MUTABILITY VS

IMMUTABILITY

MUTABLE STRUCTURES AND REACT

WHY IMMUTABLE STRUCTURES

immer

             import produce from "immer"
 const baseState = [{
   fruit: "Banana",
   favourite: false,
 }];
 const nextState = produce(baseState, draftState => {
   draftState.push({ fruit: "Tweet about it" });
   draftState[1].favourite = true;
 });
        

Immutable.js

             const baseState = Immutable.List.of({
   fruit: "Try immer",
   favourite: false,
 });
 const nextState = list.push({
   fruit: "Banana",
   favourite: false,
 });
        

MEMOIZATION

WHAT IS MEMOIZATION?

React hook - useMemo

         const expensiveCalc = length =>
  Array.from({ length }, (_v, k) => k).map(expensiveCalc);

  // inside functional component
  const { calculationsCost } = props;

  const work = useMemo(() => {
    expensiveCalc(calculationsCost);
  }, [calculationsCost]);
      

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);
          

Pure and memoized App

Pure and memoized application

WHAT MORE?

FINAL THOUGHTS

RESOURCES

THANKS FOR
LISTENING. QUESTIONS?