Re: How to optimize this JavaScript?



"Oltmans" <rolf.oltmans@xxxxxxxxx> wrote in message
news:1190551279.578231.204670@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ello all,

I'm dealing with <table> and <tr>s here and I've a function that takes
two arguments, one is the array of strings(containing <tr> ids e.g. if
we have <tr id="hello"> then our string array might contain 'hello'---
please hold on , let me explain a bit and I'm sure it will make some
sense) and other argument is the table id, which in my case is
"myTable". Now in this function I've to loop through all TRs (rows) in
this table and find those TRs whose value matches with IDs contained
in our other argument i.e. array of strings containing IDs). Here is
how my function looks like
--------------------------------------------------------------------------
----------

function findRows(idStringArray, tableID)
{

var
rows=document.getElementById("myTable").getElementByTagsName("tr");
var table=document.getElementById("myTable");

for (var i=0; i<idStringArray.length; i++)
{
for (var j=0; j<rows.length;j++)

{
if ( rows[j].ID == idStringArray[i] )
//Got the row, can now do anything with it. So we have to
store all such rows
//whose ID matches with any of the ID given in
idStringArray
}
}

}

Now problem is that I've huge number rows in a table and the above
code is taking too much time. My code above is taking some time and I
need to optimize this. Any ideas on how should I optimize this code?
Please enlighten me, I will appreciate your feedback.


Why not simply having a single loop on the string array and use the
document.getElementById function to retrieve the TR. All you need to do is
ensure the ID it is given it unique across the document.

But for academic reasons swaping the for loops around should be much quicker
since it will hit the DOM much less.

for (var j=0, rowLength = rows.length; j<rowLength;j++)
{

for (var i=0; i<idStringArray.length; i++)
{


--
Anthony Jones - MVP ASP/ASP.NET


.



Relevant Pages

  • Re: How to optimize this JavaScript?
    ... this table and find those TRs whose value matches with IDs contained ... in our other argument i.e. array of strings containing IDs). ... Why not simply having a single loop on the string array and use the ... idStringArray on every iteration. ...
    (microsoft.public.scripting.jscript)
  • Re: How to optimize this JavaScript?
    ... this table and find those TRs whose value matches with IDs contained ... in our other argument i.e. array of strings containing IDs). ... Why not simply having a single loop on the string array and use the ... idStringArray on every iteration. ...
    (microsoft.public.scripting.jscript)
  • Re: How to optimize this JavaScript?
    ... this table and find those TRs whose value matches with IDs contained ... in our other argument i.e. array of strings containing IDs). ... idStringArray on every iteration. ... The first loop does not have this ...
    (microsoft.public.scripting.jscript)
  • Re: Newbee needs once more again help with passing arrays out of a function
    ... After surfing a while I have still trouble with this array thing. ... have the following function and recive a Segmentation fault, ... llong rC = 0; ... You don't show us the definitions for PQntuples, ids and names. ...
    (comp.lang.c)
  • Re: Perl script to mimic uniq
    ... > There is a list of number sequences. ... I want to sort through the list, ... to arrays of IDs. ... There is nothing for "that array" to refer to in the previous ...
    (comp.lang.perl)

Loading