📘
Gitbook on React and NextJS
  • Welcome!
  • Performance
    • Performance Monitoring
    • Analyzing Bundle Sizes
    • How to speed up your app
  • How to avoid expensive re-calculations
  • How to decrease unnecessary re-renders
  • How to handle CPU-intensive tasks
  • Design System
    • Styled Components
    • Theme UI
    • Base Web
    • Styled JSX
    • Reach UI
    • Evergreen
  • Security
  • Third-Party Assets
  • Clickjacking
  • Cross-Site Request Forgery
  • Next JS
    • Why Next.js
    • How to style Next JS
    • How to configure Next.js
    • API in Next JS
    • How to fetch data in Next.js
    • Dynamic imports
Powered by GitBook
On this page

Was this helpful?

How to avoid expensive re-calculations

React has a built-in hook called useMemo that allows you to memoize expensive functions so that you can avoid calling them on every render.

Memoization is an optimization strategy that returns cached values from functions that have previously been invoked with the same arguments. In other words, instead of recalculating its return value, the function will return a cached value. This is useful when you have functions doing memory intensive operations and want to minimize how often they're invoked.

 const allItems = getItems(inputValue)

change to

const allItems = React.useMemo(() => getItems(inputValue), [inputValue])
PreviousHow to speed up your appNextHow to decrease unnecessary re-renders

Last updated 4 years ago

Was this helpful?