Re: How to optimize this JavaScript?
- From: "Evertjan." <exjxw.hannivoort@xxxxxxxxxxxx>
- Date: 23 Sep 2007 15:16:52 GMT
Oltmans wrote on 23 sep 2007 in microsoft.public.scripting.jscript:
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)
What is tableID used for?
{
var
rows=document.getElementById("myTable").getElementByTagsName("tr");
This will never do, since
getElementByTagsName()
does nort exist in js.
try:
getElementsByTagsName()
var table=document.getElementById("myTable");
where is table used for?
for (var i=0; i<idStringArray.length; i++)
{
for (var j=0; j<rows.length;j++)
repeating this var assignment idStringArray.length times is not useful,
perhaps takes time.
Declare j beforehand in the function.
{
if ( rows[j].ID == idStringArray[i] )
perhaps getting rows[j].ID once and putting it in a variable?
//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.
So the idea of using a huge amount of individually id-ed elements is a
flaw in itself.
Using a numerical id, like id='r123' or a row number should work better
and could be prepared by serveride scripting.
Serverside scripting could also build a alphabetical indes array,
thereby reducing the number of searches logarithmically.
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.
Dear Oltmans, better rethink the problem than just trying to optimalize
is. In general having a cliensside html table with a huge amount of rows
is contraproductive, even without javascript..
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
.
- Follow-Ups:
- Re: How to optimize this JavaScript?
- From: Evertjan.
- Re: How to optimize this JavaScript?
- References:
- How to optimize this JavaScript?
- From: Oltmans
- How to optimize this JavaScript?
- Prev by Date: How to optimize this JavaScript?
- Next by Date: Re: How to optimize this JavaScript?
- Previous by thread: How to optimize this JavaScript?
- Next by thread: Re: How to optimize this JavaScript?
- Index(es):
Relevant Pages
|
Loading