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

noticeable delay in processing a loop

P: n/a
I have the following lopp inside one of my scripts

document.getElementById('text').innerHTML = "";
for (i = 0; i < txtdef.length; i++) {
if (xx == txtdef[i][0]) {
if (yy == txtdef[i][1]) {
if (level == txtdef[i][2]) {
document.getElementById('text').innerHTML = "<P>" + txtdef[i][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?
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #2

P: n/a
JRS: In article <bq*************@ID-174912.news.uni-berlin.de>, seen in
news:comp.lang.javascript, Fabian <la****@hotmail.com> posted at Sat, 6
Dec 2003 20:37:12 :-
I have the following lopp inside one of my scripts

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


document.getElementById('text').innerHTML = "";
for (i = 0; i < txtdef.length; i++) {
T = txtdef[i]
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[i]
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3

P: n/a
Dr John Stockton hu kiteb:
document.getElementById('text').innerHTML = "";
for (i = 0; i < txtdef.length; i++) {
T = txtdef[i]
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.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #4

P: n/a
JRS: In article <bq*************@ID-174912.news.uni-berlin.de>, seen in
news:comp.lang.javascript, Fabian <la****@hotmail.com> posted at Sun, 7
Dec 2003 22:43:28 :-
Dr John Stockton hu kiteb:
document.getElementById('text').innerHTML = "";
for (i = 0; i < txtdef.length; i++) {
T = txtdef[i]
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.