[List] Rendering performance issue
I ran the following tests in chrome:
Standard MUI:
class TestListMui extends React.Component {
render() {
let items = [];
for (let i = 0; i < 10000; ++i)
items.push(<mui.ListItem key={i} primaryText={'perf-' + i}/>);
return <mui.List>
{items}
</mui.List>;
}
}
Same as MUI but with inline style:
class TestListInline extends React.Component {
render() {
let items = [];
for (let i = 0; i < 10000; ++i)
items.push(
<div key={i}>
<span
style={{
border: '10px',
boxSizing: 'border-box',
fontFamily: 'Roboto, sans-serif',
transform: 'translate3d(0px, 0px, 0px)',
color: 'rgba(0, 0, 0, 0.870588)',
fontSize: '16px',
lineHeight: '16px',
position: 'relative',
transition: 'all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms',
}}
tabIndex="0"
type="button"
>
<div>
<div style={{
marginLeft:0,
paddingLeft:16,
paddingRight:16,
paddingBottom:16,
paddingTop:16,
position:'relative',
}}
>
<div>{'perf-' + i}</div>
</div>
</div>
</span>
</div>);
return <mui.List>
{items}
</mui.List>;
}
}
Same as MUI but with CSS:
.span-aa {
border: 10px;
box-sizing: border-box;
font-family: Roboto, sans-serif;
transform: translate3d(0px, 0px, 0px);
color: rgba(0, 0, 0, 0.870588);
font-size: 16px;
line-height: 16px;
position: relative;
transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms;
}
.div-aa {
margin-left:0px;
padding-left:16px;
padding-right:16px;
padding-bottom:16px;
padding-rop:16px;
position: relative;
}
class TestListCss extends React.Component {
render() {
let items = [];
for (let i = 0; i < 10000; ++i)
items.push(
<div key={i}>
<span
className="span-aa"
tabIndex="0"
type="button"
>
<div>
<div className="div-aa"
>
<div>{'perf-' + i}</div>
</div>
</div>
</span>
</div>);
return <mui.List>
{items}
</mui.List>;
}
}
Results: MUI: 19sec Inline: 4sec CSS: 3sec
Yeap, I can confirm this. The <List /> is really slow. We even have an issue for this (https://github.com/callemall/material-ui/issues/1501).
Thanks for the link.
I'll try to see why it is so slow.
@cgestes It's not just the initial render that's slow. Consecutive renders will also be slow since we don't implement shouldComponentUpdate but we're working on that. If the initial render can be optimized for now, it would be a great help thanks :+1:
@oliviertassinari @cgestes @alitaheri Any news on this. I have a list with 300 MenuItems and it is impossible to use this right now. Any workarounds?
I have a list with 300 MenuItems and it is impossible to use this right now.
Then you know what to do! :smile:
@newoga
After reading through issues such as this I am wondering if we should try to add some performance checks to the test suite. Not 100% sure right this second how it would be implemented. Just food for thought.
(Actual performance benchmarks -- not just checking for rerenders under conditions we expect to prevent it in shouldComponentUpdate).
Yeah, I think it's a great idea. Let me know if you develop any thoughts on how we can best prepare for implementing that.
@newoga My initial thought is setting up a test bed component to help identify areas we need either pure components or classes implementing shouldComponentUpdate with the ability to record results and somehow identify regressions in the future.
- Component(s) being tested are passed in as children
- The configuration for each test should be consistent so regressions can be found, so some sort of configuration object should be implemented
- Props are passed down from the internal state of the test bed component
- After mounting, the test bed component initiates react perf,
- State is rapidly changed in a loop to trigger consecutive re-renders. (For a set amount of loops, say 100).
- After the loop is finished, react perf is stopped and wasted time is measured
- Wasted time is then compared to previous results
We can't depend on the CI server always spitting back the same results, so we need some sort of a control to run on every test as a baseline for the system that the benchmarks are currently running on. We can then use that control to assess the benchmark output.
@newoga Just to add to my last reply -- I think this is something that we can work into the browser test suite.
@nathanmarks These are good ideas.
We can't depend on the CI server always spitting back the same results, so we need some sort of a control to run on every test as a baseline for the system that the benchmarks are currently running on.
That's a good point. I suppose one option is to set up a basic dummy component that implements shouldComponetUpdate and before the other ones. I suppose for wastedTime, it might not matter if the numbers are changing with each run because ideally we can optimize to completely remove wastedTime. But for other measurements it would be beneficial to have some consistency.
Lots to think about but I think getting this component setup is a good start. Even if we can run it on CI yet, it'd be helpful to run on our development machines since the results should be consistent there.
@newoga The wasted time is good to check for to prevent PRs introducing performance regressions. (and also helps highlight a number of existing performance issues)
Obviously we will want to test the performance impact outside of wasted time too. But wasted time is the first thing to go on the chopping block ;). It's one of the biggest issues I found in my own testing.
But wasted time is the first thing to go on the chopping block ;).
@nathanmarks Definitely, I agree! I think starting with the test component to assist with identifying wasted time across the codebase is a great start.
@newoga Check this PoC out:
Text field spec to reproduce the underline and label wasting time: https://github.com/nathanmarks/material-ui/blob/textfield-performance/test/browser/TextField.spec.js
Snags on the first assertion, so spits this out:
FAILED TESTS:
TextField
✖ underline and label should not waste rendering time
PhantomJS 1.9.8 (Mac OS X 0.0.0)
AssertionError: should not waste time: expected 'TextField > TextFieldUnderline wasted 35ms across 99 instances' to equal false
There are 100 loops by default, so the 99 instances represents the wasted # of renders in this case.
I set this up to help address #2860
@nathanmarks Awesome, that was quick!
I haven't tried it yet but I look at the PoC on Github and it looks like it makes sense. That's a really cool test. I could see this eventually becoming a shouldNotWasteTime helper maybe.
@nathanmarks I'm possibly seeing opportunities to expand on this but maybe we should just leave this as is and start improving performance where we can. For example, you could probably create a PR with these fixtures and test and wrap the internal TextField components with recompose's pure HOC and see if it passes.
@newoga Yup, great idea! I had a similar thought but didn't want over-abstract until we understood more of the performance test use cases better.
One thing that makes the helper tricker is keeping the config for it concise but allowing it to performance test components with various types of prop types and combinations. The boilerplate-ish code in the TextField.spec.js test is partly to pass through the testFn and testProp, which are fairly simple for this component but would change greatly for something like <Table /> integration tests. I'm not sure what the best implementation of an abstraction is at this point due to that -- it'll come naturally once we start benchmarking different components though.
The PerfTest component will also need some expansion for more complex use cases.
Later today/tonight I'll do up a PR to fix the textfield issues presented by this example, they're annoying me :)
I'm with you :100: percent.
Sounds like a plan!
I'am delected to read about adding performance tests :)
And eventually fixing them. My current solution to render >1000 items is to use a very simple component with CSS. (From react-mdl).
As you pointed the initial render time is quite important. I wonder if using CSS like hover instead of js state change would help also. (no rerender)
I dont have a lot of time to work on the subject and wait for memoization to come in, but I definitively plan to work on the perf of multi items components.
@cgestes When I take a look at this issue I'll add in some things to help measure initial rendering time -- the issue I chose for the initial example was regarding performance with re-renders.
I'll take a look at this after I get the chance to wrap that one up.