2

I have a big object with over 10,000 key-value pairs.

I want to find the value of a specific pair so I do this:

const value = object[key]

Is this efficient? Is there a better way to do this? is it OK to have an object with < 10,000 entries?

nima
  • 7,796
  • 12
  • 36
  • 53
Kris
  • 116
  • 6
  • It's the correct way to do it. I wouldn't say having an object with 10 000 key-value-pairs is efficient, but I suppose that is a separate issue. – Emil Karlsson Sep 28 '21 at 11:11
  • 1
    If you're curious to see how property lookup is done in v8, you can see this dev blog: https://v8.dev/blog/fast-properties – Nick Parsons Sep 28 '21 at 11:23
  • 1
    Thank you both for your comments. @NickParsons thank you for the info – Kris Sep 28 '21 at 11:27

1 Answers1

6

Javascript objects are implemented as hashmaps, so the complexity of retrieving a value should be O(1), it should be efficient.

Denis_LT
  • 118
  • 7
  • Thank you @Denis_LT, I did not understand what you meant at first. I have research about it and got a grasp of what hashmaps and O(1) means. I would appreciate If you have any sources of information where I can learn more about it, please comment them – Kris Sep 28 '21 at 11:31
  • 1
    [here's a StackOverflow post talking about this](https://stackoverflow.com/questions/8877666/how-is-a-javascript-hash-map-implemented), maybe you will find more info here. Javascript has Map as a datatype now, maybe using it would be better than a plain object. The truth is you have the internet, I'm not great myself at this topic, but Google has all the information to do your own research. Here are a few Wikipedia articles about [Big(O) notation](https://en.wikipedia.org/wiki/Big_O_notation) and [Hash tables](https://en.wikipedia.org/wiki/Hash_table) to get you started. – Denis_LT Sep 28 '21 at 11:59