I want to keep eye on "which objects already were processed" by pushing them into some set.
As there is no type Set in JavaScript, I'm gonna use Object, like stated at Mimicking sets in JavaScript?
So the question is: does this dictionary-check depend on amout of objects already pushed into my Set object? Is it O(N) or O(1)?
Asked
Active
Viewed 292 times
2
-
jsperf.com would be the place to find this out – tkone Apr 03 '12 at 11:25
-
You could [create a jsPerf test case](http://jsperf.com/) to measure the speed differences between browsers and devices. – Mathias Bynens Apr 03 '12 at 11:25
1 Answers
1
The answer varies across the JS engine, definitely. Regarding V8, the check speed does depend on the amount of objects (== "Set" object property count) but it's still O(1). Otherwise no considerable web application would work fast enough.
Regarding the SO question you reference, do NOT use if ("foo" in A)
to check the element presence, if possible - this will traverse the entire prototype chain, so you will get a performance impact and most probably find a lot of objects that should not be there (like toString
). Use A.hasOwnProperty("foo")
whenever possible.

Alexander Pavlov
- 31,598
- 5
- 67
- 93
-
2Regarding the SO question you reference, do NOT use `if ("foo" in A)` to check the element presence, if possible - this will traverse the entire prototype chain, so you will get a performance impact and most probably find a lot of objects that should not be there (like `toString`). Use `A.hasOwnProperty("foo")` whenever possible. – Alexander Pavlov Apr 03 '12 at 11:27
-
You may push your comment into the answer ) Looks like people find it more useful, than the first part. – Nakilon Apr 03 '12 at 11:31
-
@AlexanderPavlov, it's 10 times faster use `if(A['foo'])` instead `hasOwnProperty`, look this perf test: http://jsperf.com/in-vs-hasownproperty-2/2 – Martin Borthiry Aug 28 '12 at 21:26
-
@MartinBorthiry: the check you suggest has a VERY limited use (no objections to the speed difference, obviously). Check `if ({"A": ""}["A"]) console.log("FOO")` or `if ({"A": 0}["A"]) console.log("FOO")` in your DevTools/Web Inspector. – Alexander Pavlov Aug 29 '12 at 14:46
-
Of course Alex, but I think that for Nakilon question is enough `obj.processed=1` – Martin Borthiry Aug 29 '12 at 14:58