1

Please help me understand how bracket notations work in the background.

Recently in a JavaScript interview, I have come across a question and when I tried solving using bracket notations, I was told that it is a very expensive operation and shouldn't be used. I didn't get more clarifications on this.

let arrayValue = ['a', 'c', 'c', 'z', 'a', 'b', 'd', 'c']
obj = {}
for (const v of arrayValue) {
  if (!obj[v]) {
    obj[v] = 1
  } else {
    obj[v] = obj[v] + 1
  }
}
console.log(obj)

When I started exploring bracket notations, I found that if I use numbers instead of strings, it sorts and rearrange the object in ascending order. But it doesn't sort it for strings.

let arrayValue = [100, 2, 4, 98, 45, 34, 67, 23, 87, 4, 19, 6, 8]
obj = {}
for (const v of arrayValue) {
  if (!obj[v]) {
    obj[v] = 1
  } else {
    obj[v] = obj[v] + 1
  }
}
console.log(obj)

I understand that according to MDN, even if I am adding a key as a number it will be converted to a string.

Bracket notation

In the object[property_name] syntax, property_name is just a string or Symbol. So, it can be any string, including 1foo, !bar!, or even (a space).

But I am not able to find out how adding key as random numbers are automatically sorting the Object as you can see in the 2nd code snippet. I have tried exploring google, MDN documents and other resources but couldn't find out, how it is done in the background.

Is it using any sorting algorithm in the background, if so why only numeric inputs are only sorted but not alphabetic inputs? Additional information like time complexity information will also help me. If you can point me to any documentation where I can read about all the information I am looking for, that will also be helpful.

Thanks in advance.

FZs
  • 11,931
  • 11
  • 28
  • 41
  • 2
    See https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order and https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties – trincot Mar 16 '21 at 20:36
  • If order matters don't use non-negative integers as object keys. JavaScript automatically will convert "1" to 1 so do something like this instead "-1" – PHP Guru Mar 16 '21 at 20:42
  • @trincot Thanks. These links helped me why number-like keys are not following insertion order. Can you help me understand why using bracket notations is considered expensive operation or thats not the case? – Raj krishna Mar 16 '21 at 20:48
  • Thanks for explanation @PHPGuru – Raj krishna Mar 16 '21 at 20:53
  • @trincot Since you have given link I have checked so many links to understand that number-like keys are not following insertion order and are always in ascending order. But no where I could find explanation how it sorts the object properties. Do you have any article or explanation for me regarding how it is sorted. Is it following any algorithm. I am mainly interested in , when creating objects with integer indices, does it have time complexity more than O(1) because if it's sorting it might be O(NlogN). Thanks – Raj krishna Mar 17 '21 at 04:26
  • 1
    The language specification does not impose a certain time complexity for property access, nor implementation details, so JavaScript engines are free to choose any implementation as long as the behaviour described in the specification is guaranteed. Note that the implementation must also take care of the prototype chain lookup. But implementations will usually use some combination of hash tables and search trees. You will not find this in the EcmaScript specification. NB: the specification *does* require sublinear access for Map/Set. But those provide insertion order. – trincot Mar 17 '21 at 08:13

0 Answers0