简体   繁体   English

反应速度慢的原因可能是什么?

[英]What might be the cause of slow react performance?

I'm supporting React-based CMS and I got stuck on the performance issue where adding 10 new input fields adds 1 second to the browser processing time. 我正在支持基于React的CMS,但我陷入了性能问题,因为添加10个新输入字段会使浏览器处理时间增加1秒。 It is not my code, and I have no experience in React. 这不是我的代码,我没有React的经验。 I have experience with profiling and have rough idea how rendering engine might work. 我有分析方面的经验,并对渲染引擎的工作原理有一个大概的了解。

I've hooked the ReactCompositeComponent._updateRenderedComponent function to see where the main performance bottleneck is, and the difference seems to be that I am getting a lot of hits. 我已经钩住了ReactCompositeComponent._updateRenderedComponent函数,以查看主要性能瓶颈在哪里,而且区别似乎是我受到了很多欢迎。 Adding 40 repeater items with calendar inputs causes this difference in performance (the form has to support over 200 inputs, which currently takes 20 seconds): 在日历输入中添加40个转发器项目会导致性能差异(表格必须支持200多个输入,当前需要20秒):

  • div : 250 ms -> 1600 ms, 1300 hits -> 8100 hits div :250毫秒-> 1600毫秒,命中1300-> 8100次
  • ConnectedField(props) : 20 ms -> 370 ms, 70 hits -> 2300 hits ConnectedField(props) :20毫秒-> 370毫秒,70次点击-> 2300次点击
  • Calendar() : 10 ms -> 300 ms, 20 hits -> 1200 hits (but i only added 40 calendar inputs) Calendar() :10毫秒-> 300毫秒,20次-> 1200次(但我只添加了40个日历输入)
  • ConnectedFieldArray() : 30 ms -> 250 ms, 200 hits -> 1300 hits ConnectedFieldArray() :30毫秒-> 250毫秒,200次-> 1300次

What might be causing so many hits for just few added elements? 是什么会导致仅添加几个元素就产生如此多的点击?

This is the Chrome DevTool profiling graph (click for full size): 这是Chrome DevTool分析图(单击以查看完整大小):

There may many reasons That effect the react performance 可能有许多原因影响反应性能

  1. Role of keys while rendering the long lists, Keys are crucial to compare the actual dom with virtual dom. 密钥在呈现长列表时的作用,密钥对于比较实际dom和虚拟dom是至关重要的。

  2. Mutating the object, If you mutate the object, Then ShouldComponentUpdate can't perform the check and triggers the updation life cycle unnecersserly. 变异对象,如果变异了对象,则ShouldComponentUpdate无法执行检查并毫不费力地触发更新生命周期。

  3. API calls only in ComponentDidMount, any other lifecycle in mounting part will call render method unnecesserly. API仅在ComponentDidMount中调用,安装部件中的任何其他生命周期都将不必要地调用render方法。

  4. Use Debounce/Throttle for input params to avoid unnecessary function/api calls 对输入参数使用去抖/油门以避免不必要的函数/ API调用

  5. Use the library React virtualized, while rendering the long lists as it will reduce drastic amount of dom elements. 使用虚拟化的React库,同时渲染长列表,因为它将减少大量的dom元素。

  6. Calculate performance with plugin react-addons-perf. 使用插件react-addons-perf计算性能。

  7. Bind Functions early, avoid binding in the render function 尽早绑定函数,避免绑定到渲染函数中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM