2

There are two alternatives I have for executing a piece of code. The code basically resets all the values in three arrays. Two of them are int arrays and one is boolean.

Choice 1

for (i = 0; i < array1.length; i++)
 array1[i] = 0;
for (i = 0; i < array2.length; i++)
 array2[i] = 0;
for (i = 0; i < array3.length; i++)
 array3[i] = false;

Choice 2

int limit = <assign the greatest of the array lengths>
for (i = 0; i < limit; i++)
{
 if (i < array1.length)
 array1[i] = 0;
 if (i < array2.length)
 array2[i] = 0;
 if (i < array3.length)
 array3[i] = false;
}

Which of these would be faster? Given that the arrays can have varying lengths and have no relationship with each other.

There is a real good discussion here. The answer listed first explains quiet well what arrays actually mean in javascript. For those interested to check it out!

asked Aug 23, 2012 at 10:04
8
  • 5
    I strongly doubt that the difference will be substantial. Don't waste your time with such mikro-optimizations. Commented Aug 23, 2012 at 10:07
  • You should measure it :) Commented Aug 23, 2012 at 10:07
  • @Niko: I am trying to learn how best to program. Hence started off with a small query like this. Dietrich has already given me good info! Commented Aug 23, 2012 at 10:10
  • +1 for testing me against root of evil Commented Aug 23, 2012 at 10:15
  • 1
    There is a thorough discussion of array initialization performance here : stackoverflow.com/questions/1295584/… Commented Aug 23, 2012 at 10:15

2 Answers 2

6

This version is almost certainly going to be faster. It is also easier to read and shorter, which are both much more important attributes than speed (99+% of the time).

for (i = 0; i < array1.length; i++)
 array1[i] = 0;
for (i = 0; i < array2.length; i++)
 array2[i] = 0;
for (i = 0; i < array3.length; i++)
 array3[i] = false;

In general, it is faster to access data that is close in memory to data that you accessed recently.

This version also does not suffer from additional branching you would get in the second version.

A sufficiently advanced compiler would transform the second version into the first.

Benchmarking: Micro-benchmarks are 100% useless without profiling your entire program first. Since I don't think the performance measurements are useful, I am not providing them.

answered Aug 23, 2012 at 10:07
Sign up to request clarification or add additional context in comments.

2 Comments

My 2c is that shorter is simply a contributing factor to easier to read/understand. If a longer alternative is, all things considered, easier to understand/clearer in its intent, then that is probably the one to go with.
My point was that any code which is simultaneously shorter and easier to understand is obviously good, whereas there is room for debate between long/simple code and short/opaque code.
1
function reset_array(array, value) {
 var len = array.length;
 array.length = 0;
 while (len--) array.push(value);
}
reset_array(array1, 0);
reset_array(array2, 0);
reset_array(array3, false);
answered Aug 23, 2012 at 10:10

4 Comments

No, this version is not as fast as using the for loop, while the code is more cleaner, it's your choice.
but again it will slow down the execution by function calling :)
@Champ: How do you know that it will slow down execution? Maybe the function will get inlined, maybe not. Maybe the version with the function gets better branch prediction, maybe not. Who knows?
may be because it contain loop so compiler may not treat it as inline function

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.