Mozilla faster than Internet Explorer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dhplank

    #1

    Mozilla faster than Internet Explorer

    Hello everyone,

    I've developed a calendar program in javascript, and up until now I've
    done most of my testing using Mozilla and Firefox. Everything works
    fine, but when I try to use Internet Explorer my response time is
    sometimes 50 times slower than using Mozilla.

    I know I haven't given you much to go by, but I'm not looking for an
    answer so much as an approach to debugging the problem. For example,
    does anyone here know of a good web site which deals with
    browser-specific javascript performance issues?

    Thanks in advance for any helpful hints.

  • RobG

    #2
    Re: Mozilla faster than Internet Explorer

    dhplank wrote:[color=blue]
    > Hello everyone,
    >
    > I've developed a calendar program in javascript, and up until now I've
    > done most of my testing using Mozilla and Firefox. Everything works
    > fine, but when I try to use Internet Explorer my response time is
    > sometimes 50 times slower than using Mozilla.
    >
    > I know I haven't given you much to go by, but I'm not looking for an
    > answer so much as an approach to debugging the problem. For example,
    > does anyone here know of a good web site which deals with
    > browser-specific javascript performance issues?
    >[/color]

    Performance differences between IE and Firefox can sometimes be found
    in unexpected places. One strategy is to isolate loops and test
    them, then split them in half, then half again, etc. until the
    really slow bits are found.

    Replacing loops like:

    for (var i=0; i<someThing.len gth; i++)
    // mess with someThing[i]
    }

    with:

    var i=0, x=someThing[i];
    do {
    // mess with x
    } while ( ( x = someThing[++i] ) )

    can cause dramatic reductions in processing time, particularly for
    large values of someThing.lengt h.

    IE tends to be slower than Firefox at DOM operations, but can be
    much faster with some others (particularly string stuff in places).

    Once you've found the slow bits, post them here and you'll likely get
    something better, or just post some of your loops that you think may
    be the cause.


    --
    Rob

    Comment

    • mike

      #3
      Re: Mozilla faster than Internet Explorer

      <snip>
      Replacing loops like:

      for (var i=0; i<someThing.len gth; i++)
      // mess with someThing[i]
      }

      with:

      var i=0, x=someThing[i];
      do {
      // mess with x
      } while ( ( x = someThing[++i] ) )

      <snip>

      Is this a "Noticeable " difference?

      Comment

      • Duncan Booth

        #4
        Re: Mozilla faster than Internet Explorer

        mike wrote:
        [color=blue]
        ><snip>
        > Replacing loops like:
        >
        > for (var i=0; i<someThing.len gth; i++)
        > // mess with someThing[i]
        > }
        >
        > with:
        >
        > var i=0, x=someThing[i];
        > do {
        > // mess with x
        > } while ( ( x = someThing[++i] ) )
        >
        ><snip>
        >
        > Is this a "Noticeable " difference?
        >
        >[/color]

        Only if '// mess with someThing[i]' repeats the expression many times, e.g.
        inside an inner loop.

        Simply replacing the for loop with:

        for (var i=0; i<someThing.len gth; i++) {
        var x = someThing[i];
        // mess with x
        }

        will give the same speed improvements without the corresponding drop in
        readability.

        There is one other way that the do..while loop gives a massive speed boost
        though:

        if any of the array elements have non-true values the while loop will exit
        when it hits the first one whereas the for loop will continue through the
        whole array. Obviously aborting the loop after a single iteration instead
        of 10,000 would give a noticeable difference. :^)

        Comment

        • RobB

          #5
          Re: Mozilla faster than Internet Explorer

          dhplank wrote:[color=blue]
          > Hello everyone,
          >
          > I've developed a calendar program in javascript, and up until now I've
          > done most of my testing using Mozilla and Firefox. Everything works
          > fine, but when I try to use Internet Explorer my response time is
          > sometimes 50 times slower than using Mozilla.
          >
          > I know I haven't given you much to go by, but I'm not looking for an
          > answer so much as an approach to debugging the problem. For example,
          > does anyone here know of a good web site which deals with
          > browser-specific javascript performance issues?[/color]

          Not particularly browser-specific, but useful:



          Comment

          • RobG

            #6
            Re: Mozilla faster than Internet Explorer

            Duncan Booth wrote:[color=blue]
            > mike wrote:
            >
            >[color=green]
            >><snip>
            >> Replacing loops like:
            >>
            >> for (var i=0; i<someThing.len gth; i++)
            >> // mess with someThing[i]
            >> }
            >>
            >> with:
            >>
            >> var i=0, x=someThing[i];
            >> do {
            >> // mess with x
            >> } while ( ( x = someThing[++i] ) )
            >>
            >><snip>
            >>
            >>Is this a "Noticeable " difference?
            >>
            >>[/color]
            >
            >
            > Only if '// mess with someThing[i]' repeats the expression many times, e.g.
            > inside an inner loop.
            >
            > Simply replacing the for loop with:
            >
            > for (var i=0; i<someThing.len gth; i++) {
            > var x = someThing[i];
            > // mess with x
            > }
            >
            > will give the same speed improvements without the corresponding drop in
            > readability.[/color]

            No it wont. Getting someThing.lengt h numerous times is slower than
            getting it once, so a similar 'for' loop is:

            for (var i=0, j=someThing.len gth; i<j; i++) {

            Using x to store the reference to someThing[i] is faster than getting
            the reference every time. Initialising x with 'var' every time is not
            required but makes no difference to the speed as far as I can tell:

            var x;
            for (var i=0, j=someThing.len gth; i<j; i++) {
            x = someThing[i];
            ...

            Pre-decrementing i is also faster in most browsers (I believe Opera is
            slower in some circumstances), so a while loop can look like:

            var i=someThing.len gth;
            var x;
            while ( i ) {
            x = someThing[--i];
            // do stuff
            }

            or

            var i=someThing.len gth;
            var x;
            while ( i-- ) {
            x = someThing[i];
            // do stuff
            }

            But it iterates through the elements of someThing backwards (which
            often is not a problem, but may be if order is important).

            Anyhow, there are a thousand variations on the theme, have a read here:

            <URL:http://www.websiteopti mization.com/speed/10/10-2.html>

            There is a bug in the page, so download it and fix the bug, then run it
            (there's a form and a function called 'testLoop').

            Also try here:

            <URL:http://4umi.com/web/javascript/optimize.htm>
            [color=blue]
            >
            > There is one other way that the do..while loop gives a massive speed boost
            > though:
            >
            > if any of the array elements have non-true values the while loop will exit
            > when it hits the first one whereas the for loop will continue through the
            > whole array. Obviously aborting the loop after a single iteration instead
            > of 10,000 would give a noticeable difference. :^)[/color]

            Not necessarily, they will only exit early if the value of the element
            is (part of) the test, e.g.:

            while(someThing[++i]) or while(( x=someThing[--i] ))

            or similar. But there are plenty of ways to avoid that, e.g. the first
            two while loops proposed above will iterate over every element.



            --
            Rob

            Comment

            • Duncan Booth

              #7
              Re: Mozilla faster than Internet Explorer

              RobG wrote:[color=blue][color=green]
              >> Simply replacing the for loop with:
              >>
              >> for (var i=0; i<someThing.len gth; i++) {
              >> var x = someThing[i];
              >> // mess with x
              >> }
              >>
              >> will give the same speed improvements without the corresponding drop
              >> in readability.[/color]
              >
              > No it wont. Getting someThing.lengt h numerous times is slower than
              > getting it once, so a similar 'for' loop is:[/color]
              <snip>

              I did say there would only be a significant improvement if there were
              multiple references to someThing[i] inside the loop. Yes, you can do a
              further micro-optimisation by precalculating the length, but unless you
              have actually timed it and found it to make a significant difference I
              wouldn't expect the difference to be huge.

              Since any kind of speed optimisation is a mistake unless you have first
              timed the code to find out where the bottlenecks actually are, I put some
              loops together and timed them:

              var testData;
              function init_test_data( ) {
              var a = [];
              for (var i = 0; i < 1000000; i++) {
              a.push('some test data');
              }
              testData = a;
              };
              init_test_data( );

              function TimingTestBase( ) {
              this.name = 'TimingTestCase ';
              }
              TimingTestBase. prototype = new TestCase;

              TimingTestBase. prototype.setUp = function() {
              this.data = testData;
              }

              function TimingTestCase( ) { };
              TimingTestCase. prototype = new TimingTestBase( );
              TimingTestCase. prototype.testL oopEmpty = function() {
              var a = this.data;
              }

              function TimingTestCase2 () { };
              TimingTestCase2 .prototype = new TimingTestBase( );
              TimingTestCase2 .prototype.test Loop1 = function() {
              var a = this.data;
              for (var i = 0; i < a.length; i++) {
              var x = a[i];
              var y= a[i];
              var z = a[i];
              }
              }

              function TimingTestCase3 () { };
              TimingTestCase3 .prototype = new TimingTestBase( );
              TimingTestCase3 .prototype.test Loop2 = function() {
              var a = this.data;
              for (var i=0, j=a.length; i<j; i++) {
              var x = a[i];
              var y= a[i];
              var z = a[i];
              }
              }

              function TimingTestCase4 () { };
              TimingTestCase4 .prototype = new TimingTestBase( );
              TimingTestCase4 .prototype.test Loop3 = function() {
              var a = this.data;
              for (var i=0, j=a.length; i<j; i++) {
              var x = a[i];
              var y= x;
              var z = x;
              }
              }

              function TimingTestCase5 () { };
              TimingTestCase5 .prototype = new TimingTestBase( );
              TimingTestCase5 .prototype.test Loop4 = function() {
              var a = this.data;
              var i = 0, j = a.length;
              while (i < j) {
              var x = a[i];
              var y= x;
              var z = x;
              i++;
              }
              }

              I ran these using ecmaunit, with a driving script looking like:
              function runTests() {
              var placeholder = document.getEle mentById('place holder');
              var cases = [TimingTestCase, TimingTestCase2 ,
              TimingTestCase3 , TimingTestCase4 , TimingTestCase5 ];
              for (var i = 0; i < cases.length; i++) {
              var testsuite = new TestSuite(new HTMLReporter(pl aceholder));
              testsuite.regis terTest(cases[i]);
              testsuite.runSu ite();
              }
              };

              (I'll gladly forward the complete files if anyone wants to play around with
              alternative structures or repeat the experiment on another browser).

              With a loop size of 100,000 IE and Mozilla showed similar times but these
              were a bit short to get reliable figures. With a loop size of 1,000,000 I
              got a reasonable runtime from Mozilla but IE crashes (or maybe just took
              longer than I was prepared to wait).

              I ran this 10 times on Mozilla 1.7.6, and noted the fastest time for each
              of the 5 tests. Halfway through the run I reversed the order in which the
              tests were run in case that made any difference.

              The times were:
              empty: 0 (as expected, just to check there wasn't any measurable
              overhead)

              Test 2: 1.082s 0%
              Test 3: 1.001s 7.5%
              Test 4: 0.711s 29%
              test 5: 0.701s 1.4%

              The percentage is the improvement shown by each test over the previous one.

              The most obvious conclusion to be drawn is that, unless you have about a
              million elements in your list, any savings to be made by changing the
              structure of the loop are probably too insignificant to measure. the
              biggest saving is made by reducing the number of array accesses, not
              suprising really since each array subscript has to be converted to a string
              as part of the lookup.

              Optimising out the a.length has about half the effect of removing one array
              subscripting operation.

              Switching from a 'for' to a 'while' loop has a minimal difference (on
              almost all runs they actually recorded the same time, so the hundredth of a
              second difference is probably not statistically significant).

              <snip>
              [color=blue][color=green]
              >> if any of the array elements have non-true values the while loop
              >> will exit
              >> when it hits the first one whereas the for loop will continue through
              >> the whole array. Obviously aborting the loop after a single iteration
              >> instead of 10,000 would give a noticeable difference. :^)[/color]
              >
              > Not necessarily, they will only exit early if the value of the
              > element is (part of) the test, e.g.:
              >
              > while(someThing[++i]) or while(( x=someThing[--i] ))
              >
              > or similar. But there are plenty of ways to avoid that, e.g. the
              > first two while loops proposed above will iterate over every
              > element.[/color]

              Yes, but I was commenting on the specific code posted which claimed to show
              a massive improvement over a for loop. My suspicion is that any improvement
              it did show was due to it short circuiting part of the loop.

              Comment

              • dhplank

                #8
                Re: Mozilla faster than Internet Explorer

                Thanks to everyone for the many helpful hints. After reading throught
                the hints and performing various tests, I've been able to establish
                that the biggest part of the performance problem has to do with how
                Mozilla and Internet Explorer handle DOM (Document Object Module)
                obects.

                For example, the following bit of code:

                <CODE>
                for (i=1; i <= 366; i++) {

                if ((document.sais ir["cal_yr1_type_j our_" + i].value != 35) ||
                (document.saisi r["cal_yr1_active _jour_" + i].value == 0)) {

                document.saisir["cal_yr1_active _jour_" + i].value = 0;
                document.saisir["cal_yr1_type_j our_" + i].value = 0;
                document.saisir["cal_yr1_debut_ jour_" + i].value = 0;
                document.saisir["cal_yr1_fin_jo ur_" + i].value = 0;
                document.saisir["cal_yr1_theor_ jour_" + i].value = 0;
                }
                if ((document.sais ir["cal_yr2_type_j our_" + i].value != 35) ||
                (document.saisi r["cal_yr2_active _jour_" + i].value == 0)) {
                document.saisir["cal_yr2_active _jour_" + i].value = 0;
                document.saisir["cal_yr2_type_j our_" + i].value = 0;
                document.saisir["cal_yr2_debut_ jour_" + i].value = 0;
                document.saisir["cal_yr2_fin_jo ur_" + i].value = 0;
                document.saisir["cal_yr2_theor_ jour_" + i].value = 0;
                }
                }
                </CODE>

                takes 3.8 seconds under Mozilla, but 36 seconds under Internet
                Explorer. And in other cases I've found that Mozilla is as much as 100
                times faster than Internet Explorer.

                In some cases I've been able to better performance by assigning a DOM
                object to a variable which I then re-use. However in a case like the
                above this doesn't seem possible (since each object is only used once).

                Anyone have any ideas?

                Comment

                • Grant Wagner

                  #9
                  Re: Mozilla faster than Internet Explorer

                  "dhplank" <dhplank@gmail. com> wrote in message
                  news:1118064648 .617342.216420@ g43g2000cwa.goo glegroups.com.. .[color=blue]
                  > Thanks to everyone for the many helpful hints. After reading throught
                  > the hints and performing various tests, I've been able to establish
                  > that the biggest part of the performance problem has to do with how
                  > Mozilla and Internet Explorer handle DOM (Document Object Module)
                  > obects.
                  >
                  > For example, the following bit of code:
                  >
                  > <CODE>
                  > for (i=1; i <= 366; i++) {
                  >
                  > if ((document.sais ir["cal_yr1_type_j our_" + i].value != 35) ||
                  > (document.saisi r["cal_yr1_active _jour_" + i].value == 0)) {
                  >
                  > document.saisir["cal_yr1_active _jour_" + i].value = 0;
                  > document.saisir["cal_yr1_type_j our_" + i].value = 0;
                  > document.saisir["cal_yr1_debut_ jour_" + i].value = 0;
                  > document.saisir["cal_yr1_fin_jo ur_" + i].value = 0;
                  > document.saisir["cal_yr1_theor_ jour_" + i].value = 0;
                  > }
                  > if ((document.sais ir["cal_yr2_type_j our_" + i].value != 35) ||
                  > (document.saisi r["cal_yr2_active _jour_" + i].value == 0)) {
                  > document.saisir["cal_yr2_active _jour_" + i].value = 0;
                  > document.saisir["cal_yr2_type_j our_" + i].value = 0;
                  > document.saisir["cal_yr2_debut_ jour_" + i].value = 0;
                  > document.saisir["cal_yr2_fin_jo ur_" + i].value = 0;
                  > document.saisir["cal_yr2_theor_ jour_" + i].value = 0;
                  > }
                  > }
                  > </CODE>
                  >
                  > takes 3.8 seconds under Mozilla, but 36 seconds under Internet
                  > Explorer. And in other cases I've found that Mozilla is as much as
                  > 100
                  > times faster than Internet Explorer.
                  >
                  > In some cases I've been able to better performance by assigning a DOM
                  > object to a variable which I then re-use. However in a case like the
                  > above this doesn't seem possible (since each object is only used
                  > once).
                  >
                  > Anyone have any ideas?[/color]

                  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. Also
                  reverse the direction of your loop (not that it makes much difference,
                  but when dealing with UI, every millisecond counts):

                  var f = document;
                  if (f && (f = f.forms) && (f = f['saisir']) && (f = f.elements)) {
                  for (var i = 367; i-- > 0;) {
                  if (f["cal_yr1_type_j our_" + i].value != 35 || /* etc */) {
                  f["cal_yr1_active _jour_" + i].value = 0;
                  // etc
                  }
                  }
                  }

                  --
                  Grant Wagner <gwagner@agrico reunited.com>
                  comp.lang.javas cript FAQ - http://jibbering.com/faq


                  Comment

                  • dhplank

                    #10
                    Re: Mozilla faster than Internet Explorer

                    >> 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. <<

                    By taking into account all the suggestions offered, I've managed to
                    dramatically improve performance. Most transactions now take less than
                    one second, which is acceptable in the context. 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). This bit
                    of code will only be executed one time per session, so perhaps I can
                    live with it, but it would be nice to speed it up if at all possible.
                    Here it is:

                    <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_j our_" + i];
                    c1de[i] = document.saisir["cal_yr1_debut_ jour_" + i];
                    c1fi[i] = document.saisir["cal_yr1_fin_jo ur_" + 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_j our_" + i];
                    c2de[i] = document.saisir["cal_yr2_debut_ jour_" + i];
                    c2fi[i] = document.saisir["cal_yr2_fin_jo ur_" + i];
                    c2th[i] = document.saisir["cal_yr2_theor_ jour_" + i];
                    }

                    EndBench("InitR ules3");

                    </code>

                    (By the way, the DoBench and EndBench subroutines are just used to time
                    the code.)

                    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. I don't think it's feasible to change the
                    design approach - too much other code is involve.

                    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.)

                    Comment

                    • Dr John Stockton

                      #11
                      Re: Mozilla faster than Internet Explorer

                      JRS: In article <1118310670.616 894.55880@o13g2 000cwo.googlegr oups.com>,
                      dated Thu, 9 Jun 2005 02:51:10, seen in news:comp.lang. javascript,
                      dhplank <dhplank@gmail. com> posted :

                      [color=blue]
                      > // 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_j our_" + i];
                      > c1de[i] = document.saisir["cal_yr1_debut_ jour_" + i];
                      > c1fi[i] = document.saisir["cal_yr1_fin_jo ur_" + 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_j our_" + i];
                      > c2de[i] = document.saisir["cal_yr2_debut_ jour_" + i];
                      > c2fi[i] = document.saisir["cal_yr2_fin_jo ur_" + i];
                      > c2th[i] = document.saisir["cal_yr2_theor_ jour_" + i];
                      > }[/color]

                      [color=blue]
                      >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.)[/color]


                      Your document.saisir must be an [array] Object; that code must look
                      up saisir in document (and there's no doubt a lot there) 3660
                      times. Try
                      It = document.saisir
                      ....
                      c1ac[i] = It["cal_yr1_active _jour_" + i];
                      which should help.

                      AIUI, i=366 ; do { ... } while (--i) could be a *little* faster.


                      You create elements in 10 arrays; it **could** be better, or worse, to
                      use one array, loaded with a ten-item object - later instead of
                      clth[i] you'd write A[i].clth .


                      You're doing calendar work, AIUI. BE SURE, if you are using Date
                      Objects repeatedly, to do your calendar in UTC rather than in local
                      time; if your systems are anything like mine, UTC is several times
                      faster IIRC. Read my site. Also, date objects may not be particularly
                      fast in comparison with efficient coding (e.g. for month-length); read
                      my site.

                      --
                      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                      <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                      Comment

                      • Thomas 'PointedEars' Lahn

                        #12
                        Re: Mozilla faster than Internet Explorer

                        dhplank wrote:

                        Please provide proper attribution.
                        vvvvvvvvvvvvvvv vvvvvvvvvvvvvvv vvvv[color=blue][color=green][color=darkred]
                        >>> Yes, take the suggestions that have already been offered and remove all[/color][/color]
                        > those accesses to document.forms['saisir'].elements[your_elements],
                        > cache a reference to the elements collection and use it directly. <<[/color]

                        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>
                        [color=blue]
                        > [...] 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_j our_" + i];
                        > c1de[i] = document.saisir["cal_yr1_debut_ jour_" + i];
                        > c1fi[i] = document.saisir["cal_yr1_fin_jo ur_" + 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_j our_" + i];
                        > c2de[i] = document.saisir["cal_yr2_debut_ jour_" + i];
                        > c2fi[i] = document.saisir["cal_yr2_fin_jo ur_" + i];
                        > c2th[i] = document.saisir["cal_yr2_theor_ jour_" + i];
                        > }[/color]

                        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.saisi r' 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_j our_" + i].value) != 35
                        || es["cal_yr1_active _jour_" + i] == 0)
                        {
                        es["cal_yr1_active _jour_" + i].value = 0;
                        es["cal_yr1_type_j our_" + i].value = 0;
                        es["cal_yr1_debut_ jour_" + i].value = 0;
                        es["cal_yr1_fin_jo ur_" + i].value = 0;
                        es["cal_yr1_theor_ jour_" + i].value = 0;
                        }

                        if (es["cal_yr2_type_j our_" + i].value != 35
                        || es["cal_yr2_active _jour_" + i].value == 0)
                        {
                        es["cal_yr2_active _jour_" + i].value = 0;
                        es["cal_yr2_type_j our_" + i].value = 0;
                        es["cal_yr2_debut_ jour_" + i].value = 0;
                        es["cal_yr2_fin_jo ur_" + 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_j our_" + 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_j our_" + i].value =
                        es["cal_yr1_debut_ jour_" + i].value =
                        es["cal_yr1_fin_jo ur_" + i].value =
                        es["cal_yr1_theor_ jour_" + i].value = 0; // x=0; y=0; <=> x = y= 0;
                        }

                        if (es["cal_yr2_type_j our_" + i].value != 35
                        || !es["cal_yr2_active _jour_" + i].value)
                        {
                        es["cal_yr2_active _jour_" + i].value =
                        es["cal_yr2_type_j our_" + i].value =
                        es["cal_yr2_debut_ jour_" + i].value =
                        es["cal_yr2_fin_jo ur_" + 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_jo ur_" + 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_jou r_" + i].value =
                        es[cal + "_fin_jour_ " + i].value =
                        es[cal + "_theor_jou r_" + 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="setZer o(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" ...>.)
                        [color=blue]
                        > 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.[/color]

                        It is not the parser, really.
                        [color=blue]
                        > I don't think it's feasible to change the design approach - too much
                        > other code is involve.[/color]

                        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.
                        [color=blue]
                        > 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.)[/color]

                        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 ...

                        Comment

                        Working...