fluentui icon indicating copy to clipboard operation
fluentui copied to clipboard

Performance drag due to styled HOC is passing new function every time on render

Open LiangMingChen opened this issue 7 years ago • 16 comments

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:

image

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)

LiangMingChen avatar Apr 18 '19 19:04 LiangMingChen

Talked to @JasonGore and he's going to look into this (thanks!).

ecraig12345 avatar Apr 18 '19 22:04 ecraig12345

Also @dzearing as FYI

ecraig12345 avatar Apr 18 '19 22:04 ecraig12345

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.

LiangMingChen avatar Apr 19 '19 00:04 LiangMingChen

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.

ecraig12345 avatar Apr 19 '19 01:04 ecraig12345

:tada:This issue was addressed in #8775, which has now been successfully released as @uifabric/[email protected].:tada:

Handy links:

msft-github-bot avatar Apr 19 '19 12:04 msft-github-bot

:tada:This issue was addressed in #8775, which has now been successfully released as @uifabric/[email protected].:tada:

Handy links:

msft-github-bot avatar Apr 19 '19 12:04 msft-github-bot

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.

JasonGore avatar Apr 19 '19 12:04 JasonGore

@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. :)

dzearing avatar Apr 19 '19 14:04 dzearing

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.

dzearing avatar Apr 19 '19 14:04 dzearing

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';
}

dzearing avatar Apr 19 '19 14:04 dzearing

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.

  1. 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) :(
  2. 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.

LiangMingChen avatar Apr 20 '19 21:04 LiangMingChen

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 avatar Apr 20 '19 22:04 LiangMingChen

@LiangMingChen Hope this is what you were after! Let me know.

dzearing avatar Apr 21 '19 19:04 dzearing

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

ecraig12345 avatar Apr 22 '19 17:04 ecraig12345

The change looks great! Thanks for fixing it.

@LiangMingChen Hope this is what you were after! Let me know.

LiangMingChen avatar Apr 22 '19 21:04 LiangMingChen

:tada:This issue was addressed in #8797, which has now been successfully released as @uifabric/[email protected].:tada:

Handy links:

msft-github-bot avatar Apr 23 '19 12:04 msft-github-bot