Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The time-complexity of using object as a dictionary in JavaScript

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.

like image 279
Wei An Avatar asked Jul 22 '11 16:07

Wei An


People also ask

Is object O 1 JavaScript?

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.

What's the time complexity of the object keys () function?

keys() is indeed O(n).

What is the time complexity of searching a key in a directory object?

That's why O(n/2) = O(n). So the linear lookup has O(n) time complexity.

Is an object in JavaScript a dictionary?

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.


1 Answers

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).

like image 60
Michael Lorton Avatar answered Sep 28 '22 01:09

Michael Lorton