简体   繁体   English

考虑使用 useMemo/useCallback 计算量大的时间限制

[英]Time limit to consider as computationally expensive for using useMemo/useCallback

According to dozens of articles, including React docs , using useCallback and useMemo hooks helps prevent unnecessary re-renders.根据包括React 文档在内的数十篇文章,使用useCallbackuseMemo钩子有助于防止不必要的重新渲染。

On the other hand, these performance-optimizing hooks are not free and come at a cost.另一方面,这些性能优化挂钩不是免费的,而是需要付费的。 And their benefit might not always offset that cost.他们的收益可能并不总能抵消该成本。 So that以便

MOST OF THE TIME YOU SHOULD NOT BOTHER OPTIMIZING UNNECESSARY RERENDERS大多数时候你不应该费心去优化不必要的渲染

said Kent C. Dodds .肯特 C说。多兹。

So there should be a tool to measure whether a function/variable is costy to compute and that cannot be relative (eg putting console.time() and console.timeEnd() before and after).所以应该有一个工具来衡量一个函数/变量是否计算成本高并且不能是相对的(例如,将console.time()console.timeEnd()放在前后)。

Is there a number as time that beyond is considered computationally heavy for most of our product users?对于我们的大多数产品用户来说,是否有超出时间的数字被认为是计算量大的?

Is there a number as time that beyond is considered computationally heavy for most of our product users对于我们的大多数产品用户来说,是否有一个数字超出了计算量?

As far as I know- there is no standard definition.据我所知,没有标准定义。 In most circumstances, its not required (even for calculations that seem complex - computers are crazy fast).在大多数情况下,它不是必需的(即使对于看起来很复杂的计算 - 计算机速度很快)。

As a general rule of thumb, if you see/notice delays in dom changes - then is the time to start looking for performance bottlenecks作为一般经验法则,如果您看到/注意到 dom 更改的延迟 - 那么是时候开始寻找性能瓶颈了

There is the option to put console logs during development and check to see how many times you see that output in the browser inspector.可以选择在开发期间放置控制台日志,并检查您在浏览器检查器中看到 output 的次数。

useMemo seems to always not do what it supposed to do and sometimes even seems to do more calculations vs using a useCallback. useMemo 似乎总是不做它应该做的事情,有时甚至似乎比使用 useCallback 做更多的计算。

There aren't any official documents from react website that show solid examples of how to correctly use useMemo. React 网站上没有任何官方文档显示如何正确使用 useMemo 的可靠示例。 It's usually the usual pseudo code such as它通常是常见的伪代码,例如

function (props) {
 const a = useMemo(() => {
 
 //some expensive calculation
 return b;
}, [props]);

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

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