Re: How to optimize this JavaScript?
- From: "Anthony Jones" <Ant@xxxxxxxxxxxxxxxx>
- Date: Mon, 24 Sep 2007 22:13:22 +0100
"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
.
- References:
- How to optimize this JavaScript?
- From: Oltmans
- How to optimize this JavaScript?
- Prev by Date: Re: Create a string with (n) copies of the same character
- Next by Date: Re: Create a string with (n) copies of the same character
- Previous by thread: Re: How to optimize this JavaScript?
- Next by thread: returnin arrays from COM to JScript
- Index(es):
Relevant Pages
|
Loading