473,394 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Performance on "for" loops

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
5 1844
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
11
by: David Morgenthaler | last post by:
How does one overide the iterator implied by the construct "for line in file:"? For example, suppose I have a file containing row,col pairs on each line, and I wish to write a subclass of file...
3
by: querypk | last post by:
Is there a better way to code nested for loops as far as performance is concerned. what better way can we write to improve the speed. for example: N=10000 for i in range(N): for j in...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
109
by: Neo | last post by:
Hi Folks,http://www.abarnett.demon.co.uk/tutorial.html#FASTFOR Page states:for( i=0; i<10; i++){ ... }i loops through the values 0,1,2,3,4,5,6,7,8,9 If you don't care about the order of the loop...
24
by: Kunal | last post by:
Hello, I need help in removing if ..else conditions inside for loops. I have used the following method but I am not sure whether it has actually helped. Below is an example to illustrate what I...
12
by: Robbie Hatley | last post by:
I'm getting a bizarre error with this code: ... case WM_COMMAND: { switch (LOWORD(wParam)) // switch (control ID) { ... case IDC_RAIN_ENABLE_SIMPLE: {
12
by: Sheldon | last post by:
Hi, I have two arrays that are of the same dimension but having 3 different values: 255, 1 or 2. I would like to set all the positions in both arrays having 255 to be equal, i.e., where one...
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
4
by: MDR | last post by:
Hello I have three "for" loops, two nested into the outer one and they depend on each other, like this: for (x=1; x<100; x++) { .... for (i=1; i<10; i++) {....} for (j=1; j<10;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.