Performance drag due to styled HOC is passing new function every time on render
Environment Information
- Package version(s): (fill this out)
- Browser and OS versions: (fill this out if relevant)
Please provide a reproduction of the bug in a codepen:
Actual behavior:

Base on react document, you should not have new lambda expression. https://github.com/airbnb/javascript/tree/master/react#methods
Bind event handlers for the render method in the constructor. eslint: react/jsx-no-bind
Why? A bind call in the render path creates a brand new function on every single render. Do not use arrow functions in class fields, because it makes them challenging to test and debug, and can negatively impact performance, and because conceptually, class fields are for data, not logic.
There is actually a tslint rule for this jsx-no-lambda: Creating new anonymous functions (with either the function syntax or ES2015 arrow syntax) inside the render call stack works against pure component rendering. When doing an equality check between two lambdas, React will always consider them unequal values and force the component to re-render more often than necessary.
you should only create new function * only when one of the prop styleProps, baseStyles, customizedStyles, this.props.styles changes.
Since styled HOC is used everywhere -- Perf show up everywhere, we should fix this ASAP.
Priorities and help requested:
Are you willing to submit a PR to fix? (Yes, No)
Requested priority: (Blocking, High, Normal, Low)
Products/sites affected: (if applicable)
Talked to @JasonGore and he's going to look into this (thanks!).
Also @dzearing as FYI
Doesn't think the change fix the
The new change at https://github.com/dzearing/office-ui-fabric-react/blob/07833d5e3e463f8332b87a33b0bfaec43647f7c9/packages/utilities/src/styled.tsx#L87
At end of day, when you return the JSX, you are giveing the new styles function (at line 84 a new function every single time. Regardless where you move the function.
Please do a profile with DetailListView to find out (which base have a componentWillReceiveProps.) https://github.com/OfficeDev/office-ui-fabric-react/issues/8764
And please add test. The merge change doesn't have a test.
Sorry to hear the fix may not work! I'm actually going to reopen this and close the new issue so we can have all the discussion in the same place.
:tada:This issue was addressed in #8775, which has now been successfully released as @uifabric/[email protected].:tada:
Handy links:
:tada:This issue was addressed in #8775, which has now been successfully released as @uifabric/[email protected].:tada:
Handy links:
Whatever the fix may be, we should be quantifying the impact with before and after numbers rather than just this guideline. While this may impact PureComponents, standard un-memoized functional components are rendered every time regardless whether or not their props change. That does not necessarily mean that components are rerendered in the DOM since React will compare and check for DOM differences. With Fabric 7 and React 16.7, functional components will become the defacto standard.
We may need to make a distinction between PureComponent impact vs. stateless functional render passes, which render (get called) on every render update regardless.
@LiangMingChen As must as I also appreciate test coverage, a test to validate that an internal function, nested within a render function is no longer created using lambda, is not a test. The lint rules, like you mentioned could be though. Perhaps you could elaborate exactly on what sort of test you're referring to. :)
And also, sorry, I did misunderstand what you were pointing at. I can fix the resolve calculation, and that I can add a test around for sure.
One other minor point. Tslint rule is certainly good, but not a catch all and I don't think it would have caught this. It doesn't walk the graph:
render() {
<>
<div>{ () => 'this will catch' }</div>
<div>{this.thisWont}</div>
</>
}
thisWont() {
return () => 'nope';
}
Thanks for fixing this.
I think is that -- yes, tslint rule or airbnb typescript guidline will not capture all the error. It point to that most react - developer's performance pitfall that''s easily fall into. (unfortunately.) To make react faster.
- the best is to make props identity check work (===). In this category, I have seen enough in my team to move "helper" function out of the render but create a new object (or new function) :(
- then the Virtual DOM diff (most of them fall into here.) -- it works, but we all need to ask, why HOC wrapped component's component render, componentDidUpdate etc get call.
Writing HOC is at its best as it composition nature of it. Then HOC is easily been most widely used. If we spend x amount time of on testing one components, if HOC is been use in 50 components, then every line should be if not double check, triple check every line on perf implication. It is hard to justify every component to be perf-watched, (if it only get use rarely.) HOC is one of those components that every line probably worth nit-pick on.
That is, to Jason Gore, not all PureComponent/Stateless component are at the same level if you just return 10 <div>s. HOC is on the different class of it own. HOC that wraps a Grid/Chart like (250+ DOM Elements easy.) and ship as a component. that's all in the different level.
For the test, I will suggested create a Simple wrapped component with componentWillReceiveProps or a static getDrivedStateFromProps https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html
to make sure that styled does not change. I found componetWillReceiveProps better to use because I can diff identity with prevProps. To make sure if any props (say items) other than styleProps, baseStyles, customizedStyles, this.props.styles that wrapped component should only get items, but not styled prop changed.
Alternative. DetailListView to find out (which base have a componentWillReceiveProps.) #8764 Grid/Chart like component (typically involved 250+ DOM Elements.) even generate and diff on the Virtual DOM is a big deal.
https://user-images.githubusercontent.com/3789716/56340877-21054f80-6168-11e9-87c4-9a3a1ffcd89c.png
@LiangMingChen Hope this is what you were after! Let me know.
Just a quick note on componentWillReceiveProps--that method is deprecated, so we can't use it for perf optimizations moving forward (though if you want to use it to demo something now, that's fine).
The change looks great! Thanks for fixing it.
@LiangMingChen Hope this is what you were after! Let me know.
:tada:This issue was addressed in #8797, which has now been successfully released as @uifabric/[email protected].:tada:
Handy links: