Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reconciliation performance function vs. class components #16112

Closed
thomasnordquist opened this issue Jul 11, 2019 · 3 comments
Closed

Reconciliation performance function vs. class components #16112

thomasnordquist opened this issue Jul 11, 2019 · 3 comments

Comments

@thomasnordquist
Copy link

Do you want to request a feature or report a bug?
neither/bug

What is the current behavior?
While migrating class components to function components (with hooks), I noticed a significant performance drop. I have a component (called TreeNodeComponent) which is part of a tree-structure. Depending on the tree size, hundreds of instance could be visible.

Wrapping the function component in a PureComponent significantly reduced the observed "render time". (From ~160ms to ~60ms)

"Render time", observed in Chrome Dev Tools

Bildschirmfoto 2019-07-11 um 16 24 10

Context:

The App renders hundreds of these TreeNodeComponent in a tree-structure. When a parent is updated, all direct children need to be reconciled.
Project: https://github.com/thomasnordquist/MQTT-Explorer

What is the expected behavior?
Comparable performance between functional and class components.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

OSX
Chromium 73 / Electron 5.0.5
React 16.8
Typescript & Webpack

@aweary
Copy link
Contributor

aweary commented Jul 11, 2019

Wrapping the function component in a PureComponent significantly reduced the observed "render time". (From ~160ms to ~60ms)

Thats expected if rendering TreeNodeComponent is expensive. React.PureComponent bails out early if props haven't changed.

While migrating class components to function components (with hooks), I noticed a significant performance drop.

Are you saying that going from a class extending React.Component to a function component caused a performance regression? Or were you already using React.PureComponent (or shouldComponentUpdate)

You can use React.memo to get the same bailout semantics with function components.

@thomasnordquist
Copy link
Author

I migrated from a React.Component with shouldComponentUpdate.

I use React.useMemo to avoid "rendering".
React.memo yields results as good as those from PureComponent. (~50-60ms)

Thanks a lot for clarifying.

@themisir
Copy link

I did not get which is preforming better: functional components or class components. What should I use for long lists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants