Skip navigation
❌ Avoid increasing time complexity by having non-normalized state variables in React .
0:00 / 0:20

❌ Avoid increasing time complexity by having non-normalized state variables in React .

George Moller

@_georgemoller117K followers

Visit on X
887 likesDownloadOriginal post

177,635 views

❌ Avoid increasing time complexity by having non-normalized state variables in React . ✅ Instead normalize the structure of the state variables to reduce time complexity.

10 Comments

Comments from the original post on X (Twitter)

  • George Moller's profile picture

    @_georgemoller

    Hey there 👋 Friendly reminder that I have more than 100 infographics and video animations just like this one. Check them out ↓

    21 likes

  • George Moller's profile picture

    @_georgemoller

    Hey there 👋 Friendly reminder that I have more than 100 infographics and video animations just like this one. Check them out ↓

    4 likes

  • Hammad Ahmad's profile picture

    @HadiPakistani

    noice , ok what if we get data from an Api call , then , we first loop all the array of object to store ids and entities, then we will again loop over the data , and then time completely will be O(n2) . i mean really? correct if i am wrong 😑 .

    8 likes

  • George Moller's profile picture

    @_georgemoller

    @ch_hadi0 Data should already come normalized from the API backend

    2 likes

  • mvrlin's profile picture

    @mvrl1n

    What’s the point of having ids and entities? Now you have O(n^2) space complexity. Just use plain object or even better Map.

    7 likes

  • Pavel Romanov's profile picture

    @pavl_ro

    I’ve seen such things in a wild and sometimes it can really boost your performance! The only thing I would change is to convert “users” to the Map. I’ve found it really useful lately

    4 likes

  • Vojtěch Jedlička's profile picture

    @lika85456

    This doesn't make much sense to me. 1. Map is designed to do exactly this. 2. There is no noticeable performance difference for smaller than huge arrays and those should be paginated anyways.

    4 likes

  • George Moller's profile picture

    @_georgemoller

    Not for smaller arrays, but try measuring the performance of the find method against the map when you have thousand of entries/records and you'll see a notable difference.

    1 like

  • ricklove's profile picture

    @Rick_00_Love

    No, don’t introduce complexity. Lookups should only be used inside an algorithm with a loop. Code review failed.

    4 likes

  • RadicalRabbit's profile picture

    @rats2riches

    Why is a seperate array of just ids needed?

    3 likes