dhplank wrote:
Please provide proper attribution.
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
>>> Yes, take the suggestions that have already been offered and remove all
> those accesses to document.forms['saisir'].elements[your_elements],
> cache a reference to the elements collection and use it directly. <<
Please learn how to quote in Usenet. `>>'...`<<' is not a widely accepted
quotation style, for one part that it confuses parsers designed to
recognize levels of quotation by one more leading character (usually `>')
and thus allow user agents to format the levels differently.
<http://jibbering.com/faq/faq_notes/pots1.html>
> [...] Caching the DOM references is what really speeded things up.
> I found though that I have to cache each reference, not just a reference
> to the form, in order to get significant improvements.
>
> This means though that I still have one bit of code, which is used to
> initialize the page, which is rather slow - it takes about 36 seconds
> under Internet Explorer (and only 3 seconds under Mozilla). [...]
>
> <code>
>
> BeginBench();
>
> // Cache DOM values for reuse.
> for (i=366; i >= 1; i--) {
> c1ac[i] = document.saisir["cal_yr1_active_jour_" + i];
> c1ty[i] = document.saisir["cal_yr1_type_jour_" + i];
> c1de[i] = document.saisir["cal_yr1_debut_jour_" + i];
> c1fi[i] = document.saisir["cal_yr1_fin_jour_" + i];
> c1th[i] = document.saisir["cal_yr1_theor_jour_" + i];
>
> c2ac[i] = document.saisir["cal_yr2_active_jour_" + i];
> c2ty[i] = document.saisir["cal_yr2_type_jour_" + i];
> c2de[i] = document.saisir["cal_yr2_debut_jour_" + i];
> c2fi[i] = document.saisir["cal_yr2_fin_jour_" + i];
> c2th[i] = document.saisir["cal_yr2_theor_jour_" + i];
> }
IMHO this is just the wrong way of "caching". You are simply "caching"
the DOM tree's "leaf" object's references into objects of built-in type,
however the lookups remain (if not increased) as well as the memory usage
does. It take it that you are trying to access a HTML form. So a
reasonable change of the original code would be to assign a (both standards
compliant and downwards compatible) reference to the HTMLFormElement object
(or its `elements' collection as you can see below) to a variable and use
that variable instead. And I sincerely doubt that you need all references
to be "cached" in an array.
`document.saisir' which should be `document.forms["saisir"] is accessed
very often and you are only accessing the form's elements. So:
// Remove redundancies and use proper references
var es = document.forms["saisir"].elements;
for (i=1; i <= 366; i++)
{
if (es["cal_yr1_type_jour_" + i].value) != 35
|| es["cal_yr1_active_jour_" + i] == 0)
{
es["cal_yr1_active_jour_" + i].value = 0;
es["cal_yr1_type_jour_" + i].value = 0;
es["cal_yr1_debut_jour_" + i].value = 0;
es["cal_yr1_fin_jour_" + i].value = 0;
es["cal_yr1_theor_jour_" + i].value = 0;
}
if (es["cal_yr2_type_jour_" + i].value != 35
|| es["cal_yr2_active_jour_" + i].value == 0)
{
es["cal_yr2_active_jour_" + i].value = 0;
es["cal_yr2_type_jour_" + i].value = 0;
es["cal_yr2_debut_jour_" + i].value = 0;
es["cal_yr2_fin_jour_" + i].value = 0;
es["cal_yr2_theor_jour_" + i].value = 0;
}
}
// Introduce proper scoping and optimize loop
for (var es = document.forms["saisir"].elements, i = 367;
--i

// 0 evaluates to `false' in a boolean expression
{
if (es["cal_yr1_type_jour_" + i].value) != 35
|| !es["cal_yr1_active_jour_" + i].value) // !0==true, !!0==false
{
es["cal_yr1_active_jour_" + i].value =
es["cal_yr1_type_jour_" + i].value =
es["cal_yr1_debut_jour_" + i].value =
es["cal_yr1_fin_jour_" + i].value =
es["cal_yr1_theor_jour_" + i].value = 0; // x=0; y=0; <=> x = y= 0;
}
if (es["cal_yr2_type_jour_" + i].value != 35
|| !es["cal_yr2_active_jour_" + i].value)
{
es["cal_yr2_active_jour_" + i].value =
es["cal_yr2_type_jour_" + i].value =
es["cal_yr2_debut_jour_" + i].value =
es["cal_yr2_fin_jour_" + i].value =
es["cal_yr2_theor_jour_" + i].value = 0;
}
}
Optional optimization steps are
for (var es = document.forms["saisir"].elements, i = 367;
--i

// 0 evaluates to `false' in a boolean expression
{
for (var j = 3; --j

{
var
cal = "cal_yr" + j,
cal_type_jour = cal + "_type_jour_" + i,
cal_active_jour = cal + "_active_jour_" + i;
if (es[cal_type_jour].value) != 35
|| !es[cal_active_jour].value) // !0==true, !!0==false
{
es[cal_active_jour].value =
es[cal_type_jour].value =
es[cal + "_debut_jour_" + i].value =
es[cal + "_fin_jour_" + i].value =
es[cal + "_theor_jour_" + i].value = 0; // x=0; y=0; <=> x = y= 0;
}
}
}
and so on.
The above code is probably triggered by an event. If yes, the even listener
should include a reference to the object that caused the event. E.g. if
the above code is executed in a method named `setZero', it could be changed
as follows:
<script type="text/javascript">
function setZero(o)
{
if ((o = o.form))
{
var e = o.elements;
// ...
}
}
}
</script>
...
<form ...>
...
<script type="text/javascript">
document.write('<input type="button" onclick="setZero(this);">');
</script>
...
</form>
Thus the form element's name does not matter anymore and two more lookups
per each loop are saved.
Further optimization can be done by having the elements have the same name.
Thus in the DOM an elements collection (an HTMLCollection object in a
standards compliant implementation) is created which removes the need for
concatenating the `i' value and for the hard-coded maximum index as it then
can be obtained reading the `length' property of that collection (where
indexes would be 0-based then). This would probably require a minor
redesign of the server-side application, though. For server-side PHP,
names ending with `[]' should be used as they become accessible as arrays
instead of plain values server-side then.
I am not sure where the `35' comes from, however this should not be
hardcoded (at least not repeatedly) as well. This change would decrease
performance a little bit (since it introduces a scope chain resolution
process), but would reduce future maintenance costs.
(And if `0' is the default value and there are no other elements (probably
the hidden `input' elements are not required), the entire script can
probably be replaced with <input type="reset" ...>.)
> I need to work with the hidden "input" fields in the HTML form, as the
> whole form is later sent to a parser which recovers everything and
> inserts it into a database.
It is not the parser, really.
> I don't think it's feasible to change the design approach - too much
> other code is involve.
Depending on what you mean with `design approach', you may or may not
have to accept that this will keep client-side processing slower than
it could be.
I wonder why you find it necessary to set the values to `0'; why do
you not use `<input type="hidden" ... value="0">'?
Furthermore, it appears that you do not take into account that there
are UAs out there where client-side scripting is unavailable and so
you have to provide a viable alternative.
> So ... any ideas on how to reduce the initialization time of this loop,
> or am I just stuck with it (get or take a few milliseconds)? Would
> getting rid of the loop significantly speed things up? (I might try
> that to see.)
Yes, it would of course, but it would possibly also remove a feature.
You are to decide whether that is acceptable or not, depending on the
reply you would give to the comment right above.
HTH
PointedEars
--
Bill Gates isn't the devil -- Satan made sure hell
_worked_ before he opened it to the damned ...