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 ?

4
  • 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? Commented Apr 30, 2012 at 6: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. Commented Apr 30, 2012 at 6:40
  • possible duplicate of "Async" loop over an object in javascript Commented Apr 30, 2012 at 6:40
  • possible duplicate of "Access non-numeric Object properties by index": stackoverflow.com/questions/7866275/… Commented Apr 30, 2012 at 6:51

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

1

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.