React Performance Mistake: useMemo Overuse

The most common React performance mistake I've seen in production? useMemo and useCallback — everywhere. Added by engineers who read that re-renders are expensive, saw a component render twice, and wrapped everything in memoisation to be safe. But useMemo isn't free. It allocates memory to cache the value. React still runs the dependency comparison on every render. If the computation is cheap — and most are — you've made it slower, not faster. 𝗠𝗲𝗺𝗼𝗶𝘀𝗮𝘁𝗶𝗼𝗻 𝗼𝗻𝗹𝘆 𝘄𝗶𝗻𝘀 𝘄𝗵𝗲𝗻 𝘁𝗵𝗲 𝗰𝗼𝘀𝘁 𝗼𝗳 𝘁𝗵𝗲 𝗰𝗼𝗺𝗽𝘂𝘁𝗮𝘁𝗶𝗼𝗻 𝗲𝘅𝗰𝗲𝗲𝗱𝘀 𝘁𝗵𝗲 𝗰𝗼𝘀𝘁 𝗼𝗳 𝘁𝗵𝗲 𝗰𝗮𝗰𝗵𝗲. Most of the time — it doesn't. And the real damage is to readability. A new engineer sees useMemo everywhere and assumes it's all load-bearing. They're afraid to touch it. The codebase calcifies. The actual culprits behind expensive re-renders are almost always simpler: 𝗦𝘁𝗮𝘁𝗲 𝗹𝗶𝘃𝗶𝗻𝗴 𝘁𝗼𝗼 𝗵𝗶𝗴𝗵 — re-rendering an entire subtree on every keystroke. Move state down. 𝗨𝗻𝘀𝘁𝗮𝗯𝗹𝗲 𝗿𝗲𝗳𝗲𝗿𝗲𝗻𝗰𝗲𝘀 — objects and arrays created inline in JSX get a new reference every render. Every child re-renders regardless of whether the data changed. 𝗖𝗼𝗻𝘁𝗲𝘅𝘁 𝗮𝗯𝘂𝘀𝗲 — one context with frequently changing values re-renders every consumer. Split contexts by update frequency. The real skill isn't knowing how to memoize. 𝗜𝘁'𝘀 𝗸𝗻𝗼𝘄𝗶𝗻𝗴 𝘄𝗵𝗲𝗻 𝘆𝗼𝘂 𝗱𝗼𝗻'𝘁 𝗻𝗲𝗲𝗱 𝘁𝗼. What performance mistake do you catch often? #React #JavaScript #Frontend #WebDevelopment #SoftwareEngineering

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories