Ivan Shirshikov’s Post

You add useMemo. The app is still slow. Before you blame React — two questions. Skip them and memo is just noise. Without useMemo, React just runs the computation. With useMemo, on every render, it checks the dependencies, saves the result in memory, and updates the dependencies. You only benefit if the dependencies stay the same. Otherwise, you’re paying for a cache that doesn’t help. Each time you use useMemo, you’re making a bet that caching will cost less than just running the computation. Most people make this bet without actually checking. Before you add useMemo, ask yourself two questions. First question: Are the dependencies stable between renders? // ❌ object created inside — reference is always new const filters = { search: input, page: current } // ✅ object from the store — reference is stable const filters = useSelector(state => state.filters) If you create a dependency inside the component, its reference changes on every render. The cache will never be used, so memo just adds extra work. If your dependencies aren’t stable, stop. Memo won’t help. Second question: Is the computation actually expensive? You don’t have to guess. There’s a clear way to check using console.time. If it takes less than 1ms, memo isn’t needed because caching takes about as long as the computation. Over 5ms, it’s worth considering. Over 16ms, you should definitely use memo. Why 16ms? Browsers render 60 frames per second, so each frame has 16ms. If a computation takes longer, users will notice lag. This is the line between a smooth UI and a visible freeze. The mistake I see most often: a developer notices slowness, adds useMemo everywhere, DevTools shows more work being done. The lag remains. They don't understand why. Memo without measurement isn't optimization. It's superstition. Measure first. Then decide. #Frontend #JavaScript #React #Vue #WebDev #Performance #SoftwareEngineering

  • No alternative text description for this image

This is a practical take. The kind of advice that only comes from actually shipping features, not just reading docs

Rule of thumb , usememo only on expensive data if passed as prop! That's the key

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories