By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,817 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,817 IT Pros & Developers. It's quick & easy.

Performance on "for" loops

P: n/a
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!
Feb 6 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 6 '06 #2

P: n/a
VK

Randy Webb wrote:
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?

Feb 6 '06 #3

P: n/a
VK said the following on 2/6/2006 10:56 AM:
Randy Webb wrote:
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.


Yes indeed.
I thought we had an agreement you don't use "array" in your schema
<quote
cite="http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/46263f60dbb73f0/15c871342785bcfa?tvc=1&q=Hikksnotathome+jagged#15c 871342785bcfa">
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.lang.javascript/browse_thread/thread/46263f60dbb73f0/b6f00f0a572647b4?q=Hikksnotathome+jagged&rnum=1#b6 f00f0a572647b4">
I said I could discuss arrays, and explain them, without referring to
"jagged", "multi-dimensional" and a "hash array".
</quote>
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 6 '06 #4

P: n/a
Randy Webb wrote:
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
Feb 7 '06 #5

P: n/a
Thomas 'PointedEars' Lahn said the following on 2/6/2006 10:39 AM:
Randy Webb wrote:
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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.