I am considering using JavaScript object as a dictionary.
var dict = {}
dict['a'] = 1;
dict['b'] = 2;
var my_first = dict['a'];
I am not clear about the time-complexity of such implementation. Is it like hashing? Thank you.
Objects are stored in the for of a key/value pair {key: value}. JavaScript objects are implemented using Hash tables under the hood. One thing about Hashing table is that — Hash tables Retrieve method have O(1), constant time complexity because of the use of a hashing function.
keys() is indeed O(n).
That's why O(n/2) = O(n). So the linear lookup has O(n) time complexity.
In Javascript, a dictionary is the same as an object. It can be initialized using the same syntax as Python. The key can be a number, a string, or an identifier. Because the dictionary is also an object, the elements can be accessed either using array notation, e.g. b[i], or using property notation, e.g. b.i.
JavaScript objects are often called "hashes" (mostly by recovering Perl addicts) or "hash tables" (unrepentant Java people). The typical look-up is somewhere between O(1) and O(log n).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With