TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

We memo all the things (2020)

108 点作者 davecardwell超过 3 年前

19 条评论

jon-wood超过 3 年前
This feels to me like another example of how the drive to use nothing but functional components in React is harming readability. Class based components were allowed to define a `shouldComponentUpdate` method which would be called ahead of rendering to decide whether a re-render is needed.<p>Having the parent component memoise the component instead feels like a step backwards as we&#x27;re now asking the parent to carry an understanding of a child component&#x27;s implementation to decide whether re-renders are needed, rather than allowing the child to communicate that up the tree.
评论 #28988240 未加载
评论 #28988900 未加载
eatonphil超过 3 年前
I like React a lot and have used it professionally for more than 5 years. First off I mostly haven&#x27;t needed to memoize any time I can remember in any enterprise (non-SaaS) production or personal apps. But surely CoinBase is at a bigger scale than my apps were&#x2F;are.<p>But if it&#x27;s the case that memoizing is such a good thing to do despite the effort (and I&#x27;m not debating that in this question), why is React designed in a way that requires you to opt into it and write the same boilerplate all over the place?<p>If hooks make this a problem maybe hooks aren&#x27;t the best (or at least pinnacle) design? (And I really prefer hooks, personally.)
评论 #28988909 未加载
评论 #28988225 未加载
评论 #28988052 未加载
jamespwilliams超过 3 年前
Kind of makes you wonder why React.memo isn&#x27;t just the default behaviour.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;facebook&#x2F;react&#x2F;issues&#x2F;14463#issuecomment-673844197" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;facebook&#x2F;react&#x2F;issues&#x2F;14463#issuecomment-...</a> suggests it was meant to be the default behaviour, but was scrapped because it would &quot;break backwards compatibility&quot;. No source is given for that claim though.
评论 #28988728 未加载
nop_slide超过 3 年前
Interesting, I mostly followed this approach which is to basically never use those constructs unless you are 100% certain you know what will happen.<p><a href="https:&#x2F;&#x2F;kentcdodds.com&#x2F;blog&#x2F;usememo-and-usecallback" rel="nofollow">https:&#x2F;&#x2F;kentcdodds.com&#x2F;blog&#x2F;usememo-and-usecallback</a><p>TLDR;<p>&gt; Specifically the cost for useCallback and useMemo are that you make the code more complex for your co-workers, you could make a mistake in the dependencies array, and you&#x27;re potentially making performance worse by invoking the built-in hooks and preventing dependencies and memoized values from being garbage collected. Those are all fine costs to incur if you get the performance benefits necessary, but it&#x27;s best to measure first.
评论 #28988418 未加载
评论 #28987989 未加载
kecupochren超过 3 年前
I find the state of things in React, pun intended, a bit sad. Hooks are definitely an improvement over class based components but it still suffers from the same issue - having logic and state in the view layer leads to spaghetti code. Not to mention dependency arrays which are easy to get wrong and the weird syntax you end up with everywhere.<p>I&#x27;m a big fan of MobX and it pains me to no end that it didn&#x27;t took off better. It&#x27;s a godsend from like 5 years ago and it makes so many of these React pain points disappear.<p>Instead of adding state to your components here and there, it works outside of the view layer. You define the model, derived computed views, actions and then just use it inside your components. You never ever worry about performance because MobX knows what is used where and it will optimize your renders automatically.<p>Moreover, dealing with state outside of the view layer makes it much more easier to refactor, reason about and test your app. Sure, you can do the same with Redux but it&#x27;s 10x more code.<p>I recommend this article on this topic by the author - <a href="https:&#x2F;&#x2F;michel.codes&#x2F;blogs&#x2F;ui-as-an-afterthought" rel="nofollow">https:&#x2F;&#x2F;michel.codes&#x2F;blogs&#x2F;ui-as-an-afterthought</a>
评论 #28989337 未加载
评论 #28989379 未加载
cheeew超过 3 年前
I&#x27;ve normally heeded to the advice from Dan Abramov (<a href="https:&#x2F;&#x2F;overreacted.io&#x2F;before-you-memo&#x2F;" rel="nofollow">https:&#x2F;&#x2F;overreacted.io&#x2F;before-you-memo&#x2F;</a>) and KCD (<a href="https:&#x2F;&#x2F;kentcdodds.com&#x2F;blog&#x2F;usememo-and-usecallback" rel="nofollow">https:&#x2F;&#x2F;kentcdodds.com&#x2F;blog&#x2F;usememo-and-usecallback</a>): mainly due to the fact that I figured performance would take a significant hit by using unnecessary React.memo &#x2F; useMemo calls throughout the codebase.<p>One negative side-effect I could see as a result of this pattern is devs becoming too reliant on these optimizations and neglecting composition.<p>But I suppose if the performance gain is substantial enough and DX isn&#x27;t negatively impacted too much, it could serve as worthwhile-- especially at the level of scale which Coinbase requires.
jasonkillian超过 3 年前
This is a great article and I agree with it fully.<p>The argument that a lot of popular React voices have made, &quot;React is fast and it&#x27;s prematurely optimizing to worry about memoizing things until a profile shows you need it&quot;, has never rung true with me. First and foremost, there&#x27;s a huge time cost to figuring out what those exact spots that need optimization are, and there&#x27;s also an educational cost with teaching less experienced engineers how to correctly identify and reason about those locations.<p>There are only two reasonable arguments for not using `memo`, `useMemo`, and `useCallback`. The first is that it decreases devx and makes the code less readable. This one is true, but it&#x27;s a very small cost to pay and clearly not the most important thing at stake as it&#x27;s only a slight net effect. The second argument is that the runtime cost of using these constructs is too high. As far as I can tell, nobody has ever done a profile showing that the runtime cost is significant at all, and the burden of proof lies with those claiming the runtime overhead is significant because it doesn&#x27;t appear that it is typically when profiling an app.<p>So, given that the two possible reasons for avoiding `memo`, `useMemo`, and `useCallback` are not convincing, and the possible downsides for not using them are fairly large, I find it best to recommend to engineering teams to just use them consistently everywhere by default.
评论 #28988183 未加载
评论 #28989057 未加载
评论 #28989254 未加载
ald890超过 3 年前
I created the benchmark for this.<p>Because in every aspect it seems that React.memo is better. Especially when we are sure of stable argument references.<p>Even when you add children to the component with memo, the worst case performance will be the same.<p><a href="https:&#x2F;&#x2F;codesandbox.io&#x2F;s&#x2F;react-memo-benchmark-m2bk6?file=&#x2F;src&#x2F;App.js" rel="nofollow">https:&#x2F;&#x2F;codesandbox.io&#x2F;s&#x2F;react-memo-benchmark-m2bk6?file=&#x2F;sr...</a>
Zababa超过 3 年前
There&#x27;s no mention of what I think is the most important point: how is this enforced? If that&#x27;s with a tool, I think it&#x27;s great and a sane way to do things. If it&#x27;s by asking everyone to remember doing it, I think it&#x27;s a missed opportunity.<p>It&#x27;s also interesting to see the age-old functional programming problem: you trade performance for ease of development. I think these days people assume that things like immutable data structures are optimised under the hood. That doesn&#x27;t seem to be the case with React, as you have to explicitly use a performance trick everywhere.<p>Their argument that it would be premature optimisation to think about where memo is not needed makes sense, it&#x27;s an interesting shift of optimisation of the runtime performance vs optimisation of the dev time.<p>&gt; Using memo and its cousins everywhere is a sane default. It’s like a mask mandate during a coronavirus pandemic. Sure, we could have everybody tested every day and only ask people who are actively contagious to wear a mask. But it’s far cheaper, simpler, and ultimately more effective to ask everybody to wear a mask by default.<p>That&#x27;s a good metaphor. It&#x27;s easier for the people who decide, by shifting the burden on everyone else. I personally get headaches by wearing a mask all the time at work. I think I may get headaches too if I had to remember something like this all the time.
评论 #28988138 未加载
meowtimemania超过 3 年前
Has anyone added a compilation step that adds all the memoization boilerplate for you?
评论 #28990977 未加载
senand超过 3 年前
This should have „React:“ mentioned in the title.<p>I was thinking about a technique to write things down…
steve_adams_86超过 3 年前
I don’t disagree that this likely improves performance in most cases, and I don’t blame the author here for any of my concerns.<p>We’ve implemented this in our code base and it’s awful. Yes it improves performance. It also makes debugging terrible. Combined with contexts, the react dev tools are virtually useless anywhere below a context with a memoized value.<p>Profiling is harder because of frameworky misdirections as well. You can do coarse benchmarks but actually following code in a profile gets noticeably worse once you memo everything.<p>I hope this is fixed. I really enjoy react, but this odd thing about it - that we arguably should memoize everything manually, and that it does make the dev tools a mess, is a huge hit to developer experience.<p>So tired of “Component did not render” in the Component tab.
jbaudanza超过 3 年前
From the useMemo docs: <a href="https:&#x2F;&#x2F;reactjs.org&#x2F;docs&#x2F;hooks-reference.html#usememo" rel="nofollow">https:&#x2F;&#x2F;reactjs.org&#x2F;docs&#x2F;hooks-reference.html#usememo</a><p><i>You may rely on useMemo as a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance.</i><p>I think about this section a lot. If they actually changed useMemo to sometimes &quot;forget&quot; values, it would break so many useEffect dependency arrays (including my own).
nathias超过 3 年前
This is an antipattern for a reason. Not only is the code less readable, it builds bad habits of code splitting state management and overall structure. A much better alternative is to just learn to understand the tools you are using better.
d23超过 3 年前
I have basically no interest in frontend stuff, but I can&#x27;t stop staring at this page. I think it&#x27;s the overall color scheme and that body font. It&#x27;s gorgeous.
have_faith超过 3 年前
It certainly feels like a failing of the hooks design that these subjects are so common (I love hooks generally!). When someone introduces a new paradigm that by design has a list of footguns to avoid you can&#x27;t help but wonder if this was necessary.<p>Has anyone tried tackling a hooks-like api that fixes the known pitfalls? encapsulating shared logic with hooks is a massive benefit but the subtleties can be difficult to teach to others.
vlunkr超过 3 年前
&gt; If you’ve ever profiled a React app – even in production mode – you know there is a non-negligible performance impact to every component that renders.<p>Maybe it&#x27;s just me, but I&#x27;ve used React for ~5 years and I&#x27;ve never needed to profile an app, since the performance out of the box has always been good enough.
评论 #28989002 未加载
Arch-TK超过 3 年前
This was in 2020. I assume the team got made redundant in the last year or something. Coinbase has genuinely been the slowest performing website I&#x27;ve used in the past couple of years.
评论 #28990602 未加载
评论 #28990261 未加载
andkon超过 3 年前
Dumb question, but is there an equivalent concern for Vue? Or a reason why this kinda checking isn&#x27;t necessary there?