1

I have a very large object that I want to only iterate through its values.

If I write a loop like below, for an object with 1m keys, the hash function and hash look up will be called 1m times. I would like to avoid this for performance reasons.

for (var key in myObj) {sum+=myObj[key]}

Can I somehow iterate only through values without caring about keys ?

Ali Salehi
  • 6,899
  • 11
  • 49
  • 75
  • 4
    Do you actually *have* a performance issue or are you just anticipating one? Have you done any measurements? Also: Why don't you use an array if you want array behavior? – Tomalak Apr 30 '12 at 06:34
  • 2
    Premature optimization is the root of all evil. Do measurements with live data and compare reality and expectation. Also you did not answer my question regarding the array option. – Tomalak Apr 30 '12 at 06:40
  • possible duplicate of ["Async" loop over an object in javascript](http://stackoverflow.com/questions/10207395/async-loop-over-an-object-in-javascript) – Joseph Apr 30 '12 at 06:40
  • possible duplicate of "Access non-numeric Object properties by index": http://stackoverflow.com/questions/7866275/access-non-numeric-object-properties-by-index – Ray Cheng Apr 30 '12 at 06:51

4 Answers4

3

I suggest there may be a misunderstanding here.

You probably know that Javascript objects are not simple arrays and their specific internal implementation is hidden from Javascript code. Objects are key-value collections, without, I believe, any guaranteed order.

How do you know that using for (var key in myObj) does hash key lookups?

I don't think there's any guarantee of that, in fact, doing that would require knowlege of the keys from which to compute the hash. Yet you are asking for all values without specifying a key.

There's a reasonable probability that for (var key in myObj) already does the closest available thing to what you want (depending on browser or js engine used). I suspect it's more likely to scan the internal structure directly and avoid calculating hashes, since you want all keys.

Someone more expert on js implementations, and standard may have more to offer on this.

joshp
  • 1,886
  • 2
  • 20
  • 28
  • The ECMAScript spec [explicitly leaves key order in objects undefined (*"implementation dependent"*)](http://stackoverflow.com/q/280713/18771), so your belief is correct. – Tomalak Apr 30 '12 at 06:48
2

For this kind of tings i suggest keeping 2 distinct arrays: one for the keys and one for the values.

i.e.

keys[0]="user"
values[0]={name:"aaa",email:".......
keys[1].....

So you can iterate over both very quickly

kappa
  • 1,559
  • 8
  • 19
1

No. On a stock JavaScript interpreter, there is no way to get the value of a property without its name.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0

No, there's no way to avoid looping using a key here. What you describe in your question looks more like an array with only numeric values, by the way. Are you sure it's an object? Then the first optimization I'd make is to use an array to calculate a sum. After that, the world is at your feet: there are many ways to loop an array.

Extra link
extra link 2

If it's really a performance issue, have a look @ Duffs Device

KooiInc
  • 119,216
  • 31
  • 141
  • 177