WebFX WebBoard
Message Thread:
View All Messages
Back to WebFX
 Optimization, Vadim, October 23, 2003
     Re:Optimization, Erik Arvidsson, October 23, 2003

Subject: Re:Optimization From: Erik Arvidsson Date: October 23, 2003
Wow that is slow. Your code is almost optimized. Is your class rule very complicated? Otherwise you should be able to loop through a table with a few 100 rows in less than a second.

function applyStyle(oTR, sClass)
var cells = otr.cells;
var l = cells.length;
for (var i = 0; i < l; i++)
cells[i].className = sClass;

I think that the problem is most likely outside this. IE is known to have problems with getting the length of the rows collection. Also looking up rows/cells can be slow so use local variables so that rows/cells does not have to be returned more than once. For example:

var c1 = tableEl.tBodies[0].rows[0].cells[0];
var c2 = tableEl.tBodies[0].rows[0].cells[0];

should be:

var cells = tableEl.tBodies[0].rows[0].cells;
var c1 = cells[0];
var c2 = cells[1];

In this case it is pretty obvious but this is very important when looping. You should never do rows/cells.length in the test that occurs in every loop iteration.


I need to color a row or some rows in a table and I do it like this
function applyStyle(oTR, sClass)
var i = 0;
var ii = oTR.cells.length;
while (i < ii) { oTR.cells[i].className = sClass; i++; }
But when I apply it to 20 or above rows it takes about 5-10 seconds
Is there a way to optimize it?


Enter your reply to this message below. HTML tags are not supported but words that start with http://, ftp:// or mailto: are converted to links.

View All Messages
Back to WebFX