-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[docs] Memoize array sorting #42010
[docs] Memoize array sorting #42010
Conversation
Netlify deploy previewhttps://deploy-preview-42010--material-ui.netlify.app/ Bundle size report |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need to optimize performance with memoization here? The tables have only 15-20 rows, except for the MaterialShowcase demo which has around 45 rows. Most of these useMemo
have dependencies that frequently change. Except for when a row is selected or all rows are selected (updating the selected
state) in which case, the component would re-render, allowing for reusing the cached rows value. What are your thoughts? @Janpot
Cc @michaldudak (#41709 (comment)) For what it’s worth, there’s already a precedent: material-ui/docs/data/material/components/table/EnhancedTable.tsx Lines 305 to 311 in ca6b5b4
so whatever direction this PR goes in should probably be consistent overall. |
This is mostly a philosophical argument, but I think in our examples we have the duty to write idiomatic React code. Our community copies and pastes from them and takes inspiration. It is technically true that in the minimal example, memoizing won't move the needle significantly in terms of performance, if it moves at all. But most derivatives of this example will turn out to be vastly more complex. While I don't want to open up the debate between "memoize all" vs. "measure before memoize", I feel like there would be a relatively broad consensus around always memoizing if the operation is >=O(n), especially if the result is not referentially stable. It's a biased viewpoint though, I don't have the bandwidth to engage in a follow-up discussion 🙂. |
Okay, in that case, I'll leave the final decision to official team members like @DiegoAndai or others. |
I usually agree that demos should include only the strictly necessary. @Janpot makes a good point that the I'm not sure what's the better option 😅, but I agree it should be consistent. I would like to know @samuelsycamore's take on this. |
Apologies for the bump, but would it be possible to agree on the approach here? |
I see valid arguments on both sides, and I think I'd lean more on the side of providing idiomatic React code. If I'm copy+pasting components into my app, I'd rather have the experience of "too much" than "not enough" to assemble a performant MVP that does all of the React-y things I need it to do. And if I do intend to implement In this case, if we move forward with the changes, maybe it would make sense to (briefly) describe the purpose of |
Any suggestions on the wording? :) |
To be clear, I don't think we should memoize everything—but I do see value in adding it to the Joy UI Table "sort and selection" demo for example, which is meant to be closer to a real-world UI, along with some text explaining why it's there and why you might (not) need it. |
I don't think adding
In summary, I wouldn't add |
In principle, premature optimization is generally bad. However, in the case of useMemo, overmemoizing shouldn't be that bad. Otherwise, React Compiler (which memoizes everything it can) wouldn't make much sense. |
@iammminzzy No decision has been made yet. Would you like to close this PR, or do you still want to propose it? I'd recommend closing it, as I don't think it's worth discussing further or involving others right now. |
Alright, I'll close it for now. |
Address #41709 (review)