0

What would be faster when it comes to iterating over all the elements?

Method 1:

let array = [10, 0, 0, 20, 1, 0, 12, 2, 0];

for(let i = 0, l = array.length; i < l; i += 3) {
    doSomething(array[i], array[i + 1], array[i + 2]);
}

vs

Method 2:

let array = [{id:10, x:0, y:0}, {id:20, x:1, y:0}, {id:12, x:2, y:0}];

for(let i = 0, l = array.length, current = null; i < l; ++i) {
    current = array[i];
    doSomething(current.id, current.x, current.y);
    // i'm aware that we could make doSomething work with the object
    // -> even a thing to consider?
}

My guess is that we're faster with 1, but you guys may have more intel of v8, spidermonkey and all that so maybe the object handling and the smaller array would be faster in the end?

10
  • Why don't you use Date.now() and see for yourself? Commented Sep 28, 2016 at 21:25
  • method 1 should be faster, but method 2 is better way to organize the data you are using. Commented Sep 28, 2016 at 21:30
  • I don't think accessing object properties this or that way make a big change in 2016. I would expect both doing the same in terms of performance. Commented Sep 28, 2016 at 21:34
  • why are you asking? best guess is the differences are likely very minor and you are most likely over optimizing Commented Sep 28, 2016 at 21:34
  • You can check by yourself using Performance.now() Commented Sep 28, 2016 at 21:36

2 Answers 2

3

Both are O(n). Nothing else matters. Even their memory consumption will only differ minimally. You can do a benchmark yourself when you think that this is performance-critical, but it most likely isn't.

Use objects because they provide a clear structure for your data and make your code more readable. Yes, you should consider passing such an object to doSomething.

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

1 Comment

thanks for talking about the intel of memory management a lil' bit, i guess i'll go with the second way then, @dadykhoff's test showed me that it wont even be critical in a game code.
0

Running both of your methods https://www.measurethat.net/ tells that Method 1 is faster by ~2,300 operations/second.

https://www.measurethat.net/Benchmarks/ShowResult/1363

5 Comments

"2300 ops/s" is pretty meaningless. Saying that snippet 2 runs about 12% slower than snippet 1 would be reasonable, but actually that benchmark is flawed like so many microbenchmarks.
that is browser and os dependent also .... i tried in 2 browsers and were reasonably close in both
Hmm thanks for the JSPerf replacement. They resulted the same in FF v50 and almost the same in Chrome v54
In measurethat.net/Benchmarks/ShowResult/1364 the second one was even faster
thanks for testing, i was planning to use this in a loop next to a render - in short - for a game, so i thought this kinda micro-optimization is necessary.

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.