DEV Community

Cover image for Optimization may overkill Performance (React)
varunprashar5
varunprashar5

Posted on

Optimization may overkill Performance (React)

๐—๐˜‚๐—ป๐—ถ๐—ผ๐—ฟ ๐——๐—ฒ๐˜ƒ: ๐—ช๐—ฒ ๐˜€๐—ต๐—ผ๐˜‚๐—น๐—ฑ ๐˜„๐—ฟ๐—ฎ๐—ฝ ๐—ฒ๐˜ƒ๐—ฒ๐—ฟ๐˜† ๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป ๐—ถ๐—ป๐˜€๐—ถ๐—ฑ๐—ฒ "๐˜‚๐˜€๐—ฒ๐—–๐—ฎ๐—น๐—น๐—ฏ๐—ฎ๐—ฐ๐—ธ" ๐—ต๐—ผ๐—ผ๐—ธ ๐˜๐—ผ ๐—ฏ๐—ผ๐—ผ๐˜€๐˜ ๐—ฝ๐—ฒ๐—ฟ๐—ณ๐—ผ๐—ฟ๐—บ๐—ฎ๐—ป๐—ฐ๐—ฒ.

๐—ฆ๐—ฒ๐—ป๐—ถ๐—ผ๐—ฟ ๐——๐—ฒ๐˜ƒ: ๐—ฆ๐—ฒ๐—ฟ๐—ถ๐—ผ๐˜‚๐˜€๐—น๐˜† (๐˜„/๐˜ ๐—ฐ๐—ต๐—ฒ๐—ฐ๐—ธ๐—ถ๐—ป๐—ด ๐—ฝ๐—ฟ๐—ผ๐—ณ๐—ถ๐—น๐—ถ๐—ป๐—ด)? ๐—œ ๐˜๐—ต๐—ถ๐—ป๐—ธ ๐—ถ๐˜ ๐˜„๐—ถ๐—น๐—น ๐—ฏ๐—ฒ ๐—ฎ ๐—ฝ๐—ฒ๐—ฟ๐—ณ๐—ผ๐—ฟ๐—บ๐—ฎ๐—ป๐—ฐ๐—ฒ ๐—ธ๐—ถ๐—น๐—น!

^ ^ In continuation to the last post (React.memo):

When a component gets a function as a prop from its parent, then whenever a parent is rendered, its child component will get re-rendered every time no matter even if you have used React.memo().

Do checkout the video for more details:
https://youtu.be/5aH_b5pUAHI

<๐—•๐—ถ๐—ด๐—Ÿ๐—ถ๐˜€๐˜ ๐—ถ๐˜๐—ฒ๐—บ๐˜€={๐—ถ๐˜๐—ฒ๐—บ๐˜€} ๐—ถ๐˜๐—ฒ๐—บ๐—–๐—น๐—ถ๐—ฐ๐—ธ={๐—ถ๐˜๐—ฒ๐—บ๐—–๐—น๐—ถ๐—ฐ๐—ธ} />

In above line, even if is passed via React.memo, It will still re-render because itemClick function will get re-created every time the parent component is rendered.

๐—œ ๐—ต๐—ฎ๐˜ƒ๐—ฒ๐—ป'๐˜ ๐—ฐ๐—ต๐—ฎ๐—ป๐—ด๐—ฒ๐—ฑ ๐˜๐—ต๐—ฒ ๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป, ๐—ฏ๐˜‚๐˜ ๐˜„๐—ต๐˜† ๐˜„๐—ถ๐—น๐—น ๐—ถ๐˜ ๐˜€๐˜๐—ถ๐—น๐—น ๐—ฟ๐—ฒ-๐—ฟ๐—ฒ๐—ป๐—ฑ๐—ฒ๐—ฟ?

This is how Javascript works, you should know that two references are never equal.

[1,2] === [1,2] //false
1==1 //true (primitives are equal)

Primitive values will be the same on every re-render but objects will be different (functions are objects in Javascript).

To prevent this, just wrap your function with useCallback hook.

๐˜‚๐˜€๐—ฒ๐—–๐—ฎ๐—น๐—น๐—ฏ๐—ฎ๐—ฐ๐—ธ(๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป, [๐—ฑ๐—ฒ๐—ฝ๐—ฒ๐—ป๐—ฑ๐—ฒ๐—ป๐—ฐ๐—ถ๐—ฒ๐˜€])
//Here we are saying to use the same function on re-renders
//If you pass an array of dependencies, then whenever any of the dependencies change, then a new function will be used.

๐——๐—ผ๐—ฒ๐˜€ ๐—ถ๐˜ ๐—บ๐—ฒ๐—ฎ๐—ป ๐˜๐—ต๐—ฎ๐˜ ๐—œ ๐˜€๐—ต๐—ผ๐˜‚๐—น๐—ฑ ๐˜„๐—ฟ๐—ฎ๐—ฝ ๐—ฒ๐˜ƒ๐—ฒ๐—ฟ๐˜† ๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป ๐—ถ๐—ป๐˜€๐—ถ๐—ฑ๐—ฒ ๐—บ๐˜† ๐—ฐ๐—ผ๐—บ๐—ฝ๐—ผ๐—ป๐—ฒ๐—ป๐˜ ๐˜„๐—ถ๐˜๐—ต ๐˜‚๐˜€๐—ฒ๐—–๐—ฎ๐—น๐—น๐—ฏ๐—ฎ๐—ฐ๐—ธ?

You should ๐—ก๐—˜๐—ฉ๐—˜๐—ฅ do that, Here are the reasons:

  1. Optimisation always does come with a cost, it's a special function that will never be garbage collected and will always have a reference in the memory.

  2. Extra check for the different values in the array of dependencies to see if these dependencies are changed then they have to recreate the newer function.

  3. Donโ€™t forget that useCallback() hook is called every time the parent component renders. Even useCallback() returns the same function object, still the inline function is re-created on every re-rendering (useCallback() just skips it).

๐—›๐—ผ๐˜„ ๐˜๐—ผ ๐—ฑ๐—ฒ๐—ฐ๐—ถ๐—ฑ๐—ฒ ๐˜„๐—ต๐—ฒ๐˜๐—ต๐—ฒ๐—ฟ ๐˜๐—ผ ๐˜‚๐˜€๐—ฒ ๐˜‚๐˜€๐—ฒ๐—–๐—ฎ๐—น๐—น๐—ฏ๐—ฎ๐—ฐ๐—ธ() ๐—ผ๐—ฟ ๐—ป๐—ผ๐˜?

There will never be a single answer to this problem, So always run your profiler and see what performance gain you will get and then decide.

Top comments (1)

Collapse
 
bryanprimus profile image
Bryan Primus Lumbantobing

please create article about how to use profiler properly and how to analyze it ๐Ÿ™