DEV Community

Discussion on: JavaScript: How to implement a dictionary/map in 3mins.

Collapse
 
juliuskoronci profile image
Igsem

{} is a dictionary by itself. Calculating the size by Object.keys().length is a very not performant and bad idea..since you are implementing a map, you need to keep a count in the state and increase it when setting and decrease it when deleting

Collapse
 
chinedu profile image
chinedu | ddevguys • Edited

Ok thanks for this. I will give it a shoot. But why do you think maintaining the length in the state has better performance than calling the length property directly on the object.keys?

Any tips on how you measure it?

Collapse
 
juliuskoronci profile image
Igsem

so object.keys will do a loop so we are talking about O(n) complexity while maintaining a count is O(1). So if we have 10000 elements in the map that is starting to be expensive. Also the map should be iterrable, I guess yours isnt yet :)

Thread Thread
 
chinedu profile image
chinedu | ddevguys

Thanks!