1

I have a JavaScript Object as:
object1 = { "abc" : "def", "ghi" : "jkl" }

Now, when I write object1["abc"]. Is this search a linear time search, i.e., O(n) or constant time search, i.e., O(1)?

Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
Devashish Dixit
  • 1,153
  • 6
  • 16
  • 25
  • probable duplicate: http://stackoverflow.com/questions/7374171/javascript-big-o-property-access-performance – leszek.hanusz Jan 27 '14 at 15:47
  • possible duplicate of [Performance of key lookup in JavaScript object](http://stackoverflow.com/questions/7700987/performance-of-key-lookup-in-javascript-object) – Trevor Dixon Jan 27 '14 at 18:21

2 Answers2

2

Accessing an array/object in Javascript is an O(1) operation.

sjkm
  • 3,887
  • 2
  • 25
  • 43
2

Well, I've made a simple program testing this, and it doesn't show constant access time. Most likely there are some other things involved - optimization or memory management or something, but it clearly shows dependence on amount of attributes.

For object with 10 attributes, it takes around 160 ms to do the 100 million accesses, for object with 100k attributes, it takes around 650 ms (on my PC with Chrome). So it doesn't look constant at all, but it is true, that for "normal" amounts of attributes it will not probably matter.

JS:

function go(amount) {
  var object1 = {};
  for (var i = 0; i < amount; i++) {
    object1['id' + i] = i;
  }

  var start = new Date().getTime();
  var j = 0;
  for (var i = 0; i < 100000000; i++) {
    j += object1['id3'];
  }
  var end = new Date().getTime();
  console.log(j);
  document.getElementById('result').innerHTML = end - start;
}

HTML:

<button onclick="go(10);">Run with 10 attributes</button>
<button onclick="go(100000);">Run with 100 000 attributes</button>
<br>
The result is <span id="result">0</span> ms

Here is the link of Fiddle

Erlik
  • 658
  • 8
  • 24
  • Interestingly enough, while the first access to a 10-attributes object is around 100ms, after you click 100k and then 10 again, it fails down to the same 700ms. Looks like a memory related issue to me. – georg Jan 27 '14 at 16:33
  • I see something different (Chrome, Win7). First clicks on 10 attr are all between 120 and 160, then any amount of clicks on 100k attr yields 600-700 ms and when I then click back to 10 attr, I do see an increase in time, but all times remain between 250 and 280 ms. – Erlik Jan 27 '14 at 16:48
  • @Erik: right, that was in Chrome 27, after I updated to 32, I see figures similar to yours. So this has been fixed in the meantime. – georg Jan 27 '14 at 17:23
  • 1
    Also, I'm getting quite different and interesting results in different browsers: http://jsperf.com/propaccess33 – georg Jan 27 '14 at 17:31
  • That's a pretty cool page. I see the same. And it shows, how IE sucks. – Erlik Jan 27 '14 at 17:35
  • I sent it to some of my friends and the results are pretty astonishing. It's hard to say, how much some optimization (which have normally marginal effect) influence the result. But.. IE 100x slower than Chrome and Opera? And Firefox 26 3x slower than Firefox 24? I am tempted to create a question about it to drag more attention to it. – Erlik Jan 28 '14 at 08:27