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

[docs] Memoize array sorting #42010

Open
wants to merge 1 commit into
base: next
Choose a base branch
from

Conversation

iammminzzy
Copy link
Contributor

@iammminzzy iammminzzy commented Apr 23, 2024

Address #41709 (review)

@mui-bot
Copy link

mui-bot commented Apr 23, 2024

Netlify deploy preview

https://deploy-preview-42010--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against 4f73f1a

@ZeeshanTamboli ZeeshanTamboli added docs Improvements or additions to the documentation enhancement This is not a bug, nor a new feature labels Apr 24, 2024
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a 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

@NMinhNguyen
Copy link
Contributor

NMinhNguyen commented Apr 24, 2024

Cc @michaldudak (#41709 (comment))

For what it’s worth, there’s already a precedent:

const visibleRows = React.useMemo(
() =>
[...rows]
.sort(getComparator(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage),
[order, orderBy, page, rowsPerPage],
);

so whatever direction this PR goes in should probably be consistent overall.

@Janpot
Copy link
Member

Janpot commented Apr 25, 2024

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.

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 🙂.

@ZeeshanTamboli
Copy link
Member

Okay, in that case, I'll leave the final decision to official team members like @DiegoAndai or others.

@DiegoAndai
Copy link
Member

DiegoAndai commented Apr 26, 2024

I usually agree that demos should include only the strictly necessary. @Janpot makes a good point that the useMemo feature will benefit the majority of users' use cases, although this can also be "covered" by adding a callout in the docs about memoization under the demos that might benefit, like these. This would be more educational and better guidance for users than blindly copying useMemo without understanding why, but people might not read it.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation enhancement This is not a bug, nor a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants