Performance on "for" loops

F

Fabian Vilers

Hi again...

I'm wondering what could be better in terms of performance between:

var my_array = new Array();
// populate array

for (index in my_array)
{
// do something with my_array[index]
}

....and...

var my_array = new Array();
// populate array
var my_array_length = my_array.length

for (i = 0; index < my_array_length; index++)
{
// do something with my_array[index]
}

Thanks for your experience!
 
R

Randy Webb

Fabian Vilers said the following on 2/6/2006 7:48 AM:
Hi again...

I'm wondering what could be better in terms of performance between:

The difference in those two loops will depend almost directly on the
array indexes used.
var my_array = new Array();
// populate array

for (index in my_array)
{
// do something with my_array[index]
}

The above loop would pick up something like this:

my_array['someName'] = "something";

Whereas the below one won't.
....and...

var my_array = new Array();
// populate array
var my_array_length = my_array.length

for (i = 0; index < my_array_length; index++)
{
// do something with my_array[index]
}

That one will fail if the array is what is called "sparse" where every
element is not defined:

my_array[1] = "something";
my_array[4] = "no 2 or 3";
Thanks for your experience!

Nothing like a good test for you to gain experience:

var my_array = new Array();
// populate array
var begin1 = new Date();
for (index in my_array)
{
// do something with my_array[index]
}
var end1 = new Date();
document.write('for-in took ' + end1-begin1 + ' ms to finish.<br>';
var my_array_length = my_array.length

var begin2 = new Date();
for (i = 0; index < my_array_length; index++)
{
// do something with my_array[index]
}
var end2 = new Date();
document.write('for-loop took ' + end2-begin2 + ' ms to finish.<br>';

A third one you might test is a while loop:

var my_array_length = my_array.length
var begin3 = new Date();
while (my_array_length--)
{
// do something with my_array[index]
}
var end3 = new Date();
document.write('while loop took ' + end3-begin3 + ' ms to finish.<br>';

And then the test will show you which is faster. Make sure your "do
something with my_array[index]" code is the same in each loop though.
 
T

Thomas 'PointedEars' Lahn

Randy said:
Fabian Vilers said the following on 2/6/2006 7:48 AM:
var my_array = new Array();
// populate array
var my_array_length = my_array.length

for (i = 0; index < my_array_length; index++)
{
// do something with my_array[index]
}

That one will fail if the array is what is called "sparse" where every
element is not defined:

my_array[1] = "something";
my_array[4] = "no 2 or 3";

No, it will not.


PointedEars
 
V

VK

Randy said:
That one will fail if the array is what is called "sparse" where every
element is not defined:

"where *not* every element is defined", that was your mental intention
to say.

I thought we had an agreement you don't use "array" in your schema, and
I don't show the flows in your schema. Are we going to break the deal?
 
R

Randy Webb

VK said the following on 2/6/2006 10:56 AM:
"where *not* every element is defined", that was your mental intention
to say.

Yes indeed.
I thought we had an agreement you don't use "array" in your schema

<quote
cite="http://groups.google.com/group/comp...vc=1&q=Hikksnotathome+jagged#15c871342785bcfa">
I can answer *any* question not directly related to mutil-dimensional
arrays without using any one of those three phrases, and in fact I
wouldn't even consider using any of those terms unless it was directly
related to the question.
</quote>

And then I clarified it even further:

<quote
cite="http://groups.google.com/group/comp...Hikksnotathome+jagged&rnum=1#b6f00f0a572647b4">
I said I could discuss arrays, and explain them, without referring to
"jagged", "multi-dimensional" and a "hash array".
and I don't show the flows in your schema.

If there is a flaw in what I said in this thread about arrays, then
please point it out. You showed one where I said something in a way that
was obscure and could have been worded better. That is always to the
best of anybody reading the thread. If there is anything else wrong,
then feel free to correct it.
Are we going to break the deal?

I have not broken it :) I have not used those three terms/phrases unless
they were *directly related* to the topic at hand.
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 2/6/2006 10:39 AM:
Randy said:
Fabian Vilers said the following on 2/6/2006 7:48 AM:
var my_array = new Array();
// populate array
var my_array_length = my_array.length

for (i = 0; index < my_array_length; index++)
{
// do something with my_array[index]
}
That one will fail if the array is what is called "sparse" where every
element is not defined:

my_array[1] = "something";
my_array[4] = "no 2 or 3";

No, it will not.

Yes, it will fail in the sense that it will not do what you would think
it would do. Had it not been for your desire to be pedantic about my
response you would not have felt it necessary to point that out. What
you didn't point out was the use of different variables in the for loop
though. The i=0 in the example above is unneeded.

So, let me phrase it in a way that you might be able to understand it,
given your displayed inability to comprehend simple English:

A for-loop will not perform as expected if it is used on a sparse array.
It will loop over undefined array elements and that could possibly skew
any calculations/procedures you may attempt to perform using those array
entries.

Now, just for you, a code snippet that displays where it will "fail".

<script type="text/javascript">
var my_array = new Array()
my_array[0] = 1
my_array[1000] = 2
var my_array_length = my_array.length
var counter = 0
for (var index = 0; index < my_array_length; index++)
{counter = counter + my_array[index]}
alert(counter)
</script>

The desired results would be 3, the actual result will be NaN.

Also, not the lack of any semi-colons at the end of any statement in my
code other than in the for loop definition.

BTW, in the future, I would appreciate it if you kept your pedantic
bullshit noise creating post to yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top