noticeable delay in processing a loop

F

Fabian

I have the following lopp inside one of my scripts

document.getElementById('text').innerHTML = "";
for (i = 0; i < txtdef.length; i++) {
if (xx == txtdef[0]) {
if (yy == txtdef[1]) {
if (level == txtdef[2]) {
document.getElementById('text').innerHTML = "<P>" + txtdef[3] +
"</P>";
}
}
}
}

txtdef is an array. In this case, it only has about 4 rows, but there is
no theoretical limit. However, there is a noticeable delay in processing
when this sequence is triggered. I was wondering if there is a more
efficient way of writing this routine?
 
F

Fabian

Fabian hu kiteb:
I have the following lopp inside one of my scripts
txtdef is an array. In this case, it only has about 4 rows, but there
is no theoretical limit. However, there is a noticeable delay in
processing when this sequence is triggered. I was wondering if there
is a more efficient way of writing this routine?

I found the principal answer. writing that text was causing the document
to reflow, and it was the reflowing that caused the slowdown. Judicious
use of div has now removed that cause of delay.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
I have the following lopp inside one of my scripts

document.getElementById('text').innerHTML = "";
for (i = 0; i < txtdef.length; i++) {
if (xx == txtdef[0]) {
if (yy == txtdef[1]) {
if (level == txtdef[2]) {
document.getElementById('text').innerHTML = "<P>" + txtdef[3] +
"</P>";
}
}
}
}


document.getElementById('text').innerHTML = "";
for (i = 0; i < txtdef.length; i++) {
T = txtdef
if (xx == T[0]) {
if (yy == T[1]) {
if (level == T[2]) {
document.getElementById('text').innerHTML = "<P>" + T[3] + "</P>";
}
}
}
}


should make a small difference, and is a good idea anyway.

document.getElementById('text').innerHTML = "";
for (i = 0; i < txtdef.length; i++) {
T = txtdef
if ( xx == T[0] && yy == T[1] && level == T[2] ) {
document.getElementById('text').innerHTML = "<P>" + T[3] + "</P>";
}
}

is IIRC equivalent, and could be a bit quicker.

But I've seen your second post in this thread.

document.getElementById('text').innerHTML = "";
for (i = 0; i < txtdef.length; T = txtdef[i++])
if ( xx == T[0] && yy == T[1] && level == T[2] )
document.getElementById('text').innerHTML = "<P>" + T[3] + "</P>";

For a further improvement, note that only the last assignment has
effect.

document.getElementById('text').innerHTML = "";
for (i = txtdef.length; i >=0 ; T = txtdef[--i])
if ( xx == T[0] && yy == T[1] && level == T[2] ) {
document.getElementById('text').innerHTML = "<P>" + T[3] + "</P>" ;
break }

S = "" ;
for (i = txtdef.length; i > 0 ; T = txtdef[--i])
if ( xx == T[0] && yy == T[1] && level == T[2] ) {
S = "<P>" + T[3] + "</P>" ;
break }
document.getElementById('text').innerHTML = S ;



All untested.
 
F

Fabian

Dr John Stockton hu kiteb:
document.getElementById('text').innerHTML = "";
for (i = 0; i < txtdef.length; i++) {
T = txtdef
if ( xx == T[0] && yy == T[1] && level == T[2] ) {


Testing all these if statements at the same time is faster than testing
them in series? I would be very interested in a definitive answer to
this one.
S = "" ;
for (i = txtdef.length; i > 0 ; T = txtdef[--i])
if ( xx == T[0] && yy == T[1] && level == T[2] ) {
S = "<P>" + T[3] + "</P>" ;
break }
document.getElementById('text').innerHTML = S ;



All untested.

Tested, and working quite happily. Thank you. Improvements are slight
compared to when I removed the text reflow situation, but still useful.
The overall javascript throws around so many images that any little
speed increase is useful.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Dr John Stockton hu kiteb:
document.getElementById('text').innerHTML = "";
for (i = 0; i < txtdef.length; i++) {
T = txtdef
if ( xx == T[0] && yy == T[1] && level == T[2] ) {


Testing all these if statements at the same time is faster than testing
them in series? I would be very interested in a definitive answer to
this one.


In each case, the testing can stop once the result is known. In a
compiled language that permits partial boolean evaluation, the above
approach should certainly be quicker. In javascript, it might or might
not be. Certainly, if xx!=T[0] there is no need to test further &&
items.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top