React performance tip – use ReactDOM.createRoot() instead of ReactDOM.render()

šŸ“– Neural networks for Javascript developers

The Neural Networks for JavaScript developers book is almost ready! Learn the basics of AI with TensorFlowJs examples. Join now the presale and get a 15$ coupon off the launching price!

The ReactDOM.createRoot() comes as a replacement for the ReactDOM.render().

Among multiple other benefits using the createRoot ads the automatic batching of multiple setStates calls. This reduces the number of render cycles a component needs, therefore improving performance.

For example, let's take the below React component:

function MyComponent({descriptor}) {
  const [count, setCount] = useState(0)
  const [flag, setFlag] = useState(false)
  const renders = useRef(0)

  useEffect( () => {renders.current++})

  const onClickHandler = ()=> {
    Promise.resolve(100).then(
      ()=> {
        setCount(c => c+1)
        setFlag(f => !f)
      }
    )
  }

  return(<>
      <p>{descriptor} renders = {renders.current}</p>
      <button onClick={onClickHandler}>Add ({count})</button>
  </>)
}

The component makes 2 asynchronous calls to setState and tracks the total number of how many times that React component rerendered.

It looks like this:
performance ReactDOM.createRoot() vs ReactDOM.render()

Now, lets render this component both with ReactDOM.createRoot() and ReactDOM.render():

// rendering the compoent with render()
ReactDOM.render(
  <MyComponent descriptor="ReactDOM.render()" />, 
  document.getElementById("id1")
)

// rendering the compoent with createRoot()
const root = ReactDOM.createRoot(document.getElementById("id2"))
root.render(<MyComponent descriptor="ReactDOM.createRoot()" />)

Even if we have the exact same component, using ReactDOM.createRoot() will result in 50% fewer renderings, as it batches multiple async setStates calls.
performance ReactDOM.createRoot() vs ReactDOM.render()

You can checkout the full codepen with this example here.

In conclusion, using ReactDOM.createRoot() instead of ReactDOM.render() can greatly improve the performance of your React application and as demonstrated in the example, it can result in fewer render cycles.

šŸ“– Neural networks for Javascript developers

The Neural Networks for JavaScript developers book is almost ready! Learn the basics of AI with TensorFlowJs examples. Join now the presale and get a 15$ coupon off the launching price!


Leave a Reply

Your email address will not be published. Required fields are marked *

Home Screencasts Best of Newsletter Search X

Neural Networks for JavaScript developers
Presale - 15$ free coupon

Hi friend! Before you go, just wanted to let you know that in March 2023 I will be launching the TensorFlow.Js by Example course.

This course will include basic concepts of AI and Neural Networks done with TensorFlowJs such as:

  • Working with datasets
  • Visualizing training
  • Deep Neural Networks in JavaScript
  • Reinforcement Learning
  • Convolutional neural networks
  • and much more ...

Also, there will be a lot of examples as:

  • Object detection
  • Natural language processing
  • Face and voice recognition
  • Gaming AI

Join now the waiting list and get a 15$ coupon off the launching price!

X