0

I have the following object:

let obj = {
  "name": "someName",
  "age": 33,
  "city": "someCity"
}

I understand that an object is not indexed

"JS objects have no defined order, they are (by definition) an unsorted set of key-value pairs."

so what is the complexity of:

let result = obj.name

Is it searching all over the object to find the right key? Is there a way to index this object so complexity will be O(1)?

Ivar
  • 6,138
  • 12
  • 49
  • 61
larry ckey
  • 141
  • 2
  • 14
  • 1
    I'm 99% sure that, unless JS has some weird quirk, the complexity is O(1), and that it doesn't search because it uses a hash table. https://en.wikipedia.org/wiki/Hash_table – Nisala May 09 '21 at 13:56

1 Answers1

0

Data access which involves retrieving or modifying data stored in an object is done in constant time O(1). This is also true for the insertion and removal of data.

Junaed Siam
  • 344
  • 1
  • 7