468,556 Members | 2,376 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,556 developers. It's quick & easy.

CPU 100% on a Loop while retrieving form values

Hi,

i have a simple problem. i have a form with a lot's of number of Text
and Hidden Fields. I just do a simple loop to get each value for each
attribute. I have about more than 2000.

if i do something simple like :

for (var i=0; i<2000; i++)
aValue = i+10;

it runs well. But if i do something like
for (var i=0; i<2000; i++)
aValue = document.myForm.elements[i].value;

it gives me 100% CPU for a long time before i submit the form.

Cheers,

Kant.
Jul 20 '05 #1
3 1448
"Kant" <ka******@hotmail.com> wrote in message
news:7e**************************@posting.google.c om...
i have a simple problem. i have a form with a lot's of number
of Text and Hidden Fields. I just do a simple loop to get each
value for each attribute. I have about more than 2000.

if i do something simple like :

for (var i=0; i<2000; i++)
aValue = i+10;

it runs well. But if i do something like
for (var i=0; i<2000; i++)
aValue = document.myForm.elements[i].value;

it gives me 100% CPU for a long time before i submit the form.


It is very inefficient to resolve document.myForm.elements 2000 times.
Compare it with:-

var els = document.forms['myForm'].elements;
for(var c = 0; c < 2000;c++){
aValue = els[c].value;
}

But looking up properties on 2000 object is going to be relatively slow
however you do it.

Richard.
Jul 20 '05 #2
Lee
Kant said:

Hi,

i have a simple problem. i have a form with a lot's of number of Text
and Hidden Fields. I just do a simple loop to get each value for each
attribute. I have about more than 2000.

if i do something simple like :

for (var i=0; i<2000; i++)
aValue = i+10;

it runs well. But if i do something like
for (var i=0; i<2000; i++)
aValue = document.myForm.elements[i].value;

it gives me 100% CPU for a long time before i submit the form.


Move as much as possible to outside the loop:

var myElements=document.myForm.elements;
for(var i=0;i<2000;i++){
aValue=myElements[i].value;
}

Slightly more efficient, but probably not practical in whatever
you're really doing:

var myElements=document.myForm.elements;
for(var i=0;i<2000;aValue=myElements[i++].value);

Jul 20 '05 #3
Ok thanks a lot guys :D

it works fine !!!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by kaptain kernel | last post: by
3 posts views Thread by Shiraj | last post: by
3 posts views Thread by giant food | last post: by
1 post views Thread by Captain Dondo | last post: by
10 posts views Thread by Girish | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.