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

[tree view] Should use maps not arrays for selected / expanded #12283

Open
burtonator opened this issue May 1, 2020 · 8 comments
Open

[tree view] Should use maps not arrays for selected / expanded #12283

burtonator opened this issue May 1, 2020 · 8 comments
Labels
component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! discussion MUI X performance

Comments

@burtonator
Copy link

burtonator commented May 1, 2020

Right now selected/expanded are arrays and not maps/dictionaries.

Arrays are linear time lookup. Maps are constant time... O(N) vs O(1)...

For smaller maps it won't matter but for bigger maps performance will be impacted.

Search keywords:

@joshwooding joshwooding added component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! new feature New feature or request discussion and removed new feature New feature or request labels May 1, 2020
@esseswann
Copy link

esseswann commented May 2, 2020

This line shows that there is indeed an array look up for finding if the item is expanded
https://github.com/mui-org/material-ui/blob/e04b4634f4b72f564c4e7532c1c91c15ab4fd5eb/packages/material-ui-lab/src/TreeView/TreeView.js#L39

But firstly Arrays in JavaScript are known to have faster lookup time when they have small length and secondly both in terms of performance and proper type usage you should go with Set

@eps1lon
Copy link
Member

eps1lon commented May 2, 2020

An array is easier to handle for the basic use case. IMO <TreeView expanded={[1, 2]} /> is easier to parse than <TreeView expanded={{1: true, 2: true}} />. We could switch to other data structures internally but this adds the cost of transforming these data types.

In the end it boils down to: Are there real-world use cases where dictionaries make a noticeable difference?

@oliviertassinari
Copy link
Member

oliviertassinari commented May 2, 2020

We could switch to other data structures internally but this adds the cost of transforming these data types.

I was interested in testing this approach out, it seems that it's a great approach.
Here is a quick and dirty benchmark, assuming somebody tries to render 100,000 items:

var expanded1 = Array.from(new Array(100000)).map((_, index) => String(index))

console.time('conversion')
var expanded2 = expanded1.reduce((acc, index) => {
    acc[index] = true;
    return acc;
}, {});
console.timeEnd('conversion')

console.time('array')
expanded1.indexOf('foo')
console.timeEnd('array')

console.time('object')
expanded['foo']
console.timeEnd('object')

conversion: 4.6630859375ms
array: 0.3330078125ms
object: 0.001953125ms

It looks like that if we hit a performance issue, we can handle the concern internally, 20 items are enough to convert the data structure to an object and observe an ROI > 1. So I don't think that performance should be a concern from the API standpoint.


This makes me think, virtualization support could be pretty neat!

@eps1lon
Copy link
Member

eps1lon commented May 3, 2020

  1. Objects are likely the worst choice here. Set and Map should have better runtime. Adding this many properties to objects has likely some memory overhead.
  2. .reduce is over-engineered. A simple forEach does the same.
  3. Micro-benchmarks are not useful for this issue. We want to know the actual performance impact on render/user interactions.
  4. console.time is inaccurate for micro benchmarks. Try performance.now or, better, jsperf.com

@joshwooding
Copy link
Member

I had similar thoughts on this, I had previously discussed using a Set but decided against it since I did not have an example where I could measure the actual performance impact as @eps1lon suggested.

@rashdeva
Copy link

rashdeva commented May 8, 2020

We also require virtualization for Tree View. Most of the time, Tree VIew has many items inside.

@oliviertassinari
Copy link
Member

@rash2x Could you open a new issue for Virtualization? Thanks

@rashdeva
Copy link

rashdeva commented May 8, 2020

@oliviertassinari done! #9685

@oliviertassinari oliviertassinari changed the title tree view should use maps not arrays for selected / expanded [TreeView] Should use maps not arrays for selected / expanded Jun 16, 2020
@flaviendelangle flaviendelangle transferred this issue from mui/material-ui Mar 1, 2024
@oliviertassinari oliviertassinari changed the title [TreeView] Should use maps not arrays for selected / expanded [tree view] Should use maps not arrays for selected / expanded Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! discussion MUI X performance
Projects
None yet
Development

No branches or pull requests

6 participants