noticeable delay in processing a loop

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

    noticeable delay in processing a loop

    I have the following lopp inside one of my scripts

    document.getEle mentById('text' ).innerHTML = "";
    for (i = 0; i < txtdef.length; i++) {
    if (xx == txtdef[i][0]) {
    if (yy == txtdef[i][1]) {
    if (level == txtdef[i][2]) {
    document.getEle mentById('text' ).innerHTML = "<P>" + txtdef[i][3] +
    "</P>";
    }
    }
    }
    }

    txtdef is an array. In this case, it only has about 4 rows, but there is
    no theoretical limit. However, there is a noticeable delay in processing
    when this sequence is triggered. I was wondering if there is a more
    efficient way of writing this routine?


    --
    --
    Fabian
    Visit my website often and for long periods!


  • Fabian

    #2
    Re: noticeable delay in processing a loop

    Fabian hu kiteb:
    [color=blue]
    > I have the following lopp inside one of my scripts[/color]
    [color=blue]
    > txtdef is an array. In this case, it only has about 4 rows, but there
    > is no theoretical limit. However, there is a noticeable delay in
    > processing when this sequence is triggered. I was wondering if there
    > is a more efficient way of writing this routine?[/color]

    I found the principal answer. writing that text was causing the document
    to reflow, and it was the reflowing that caused the slowdown. Judicious
    use of div has now removed that cause of delay.


    --
    --
    Fabian
    Visit my website often and for long periods!


    Comment

    • Dr John Stockton

      #3
      Re: noticeable delay in processing a loop

      JRS: In article <bqseqj$256tov$ 1@ID-174912.news.uni-berlin.de>, seen in
      news:comp.lang. javascript, Fabian <lajzar@hotmail .com> posted at Sat, 6
      Dec 2003 20:37:12 :-[color=blue]
      >I have the following lopp inside one of my scripts
      >
      > document.getEle mentById('text' ).innerHTML = "";
      > for (i = 0; i < txtdef.length; i++) {
      > if (xx == txtdef[i][0]) {
      > if (yy == txtdef[i][1]) {
      > if (level == txtdef[i][2]) {
      > document.getEle mentById('text' ).innerHTML = "<P>" + txtdef[i][3] +
      >"</P>";
      > }
      > }
      > }
      > }[/color]

      document.getEle mentById('text' ).innerHTML = "";
      for (i = 0; i < txtdef.length; i++) {
      T = txtdef[i]
      if (xx == T[0]) {
      if (yy == T[1]) {
      if (level == T[2]) {
      document.getEle mentById('text' ).innerHTML = "<P>" + T[3] + "</P>";
      }
      }
      }
      }


      should make a small difference, and is a good idea anyway.

      document.getEle mentById('text' ).innerHTML = "";
      for (i = 0; i < txtdef.length; i++) {
      T = txtdef[i]
      if ( xx == T[0] && yy == T[1] && level == T[2] ) {
      document.getEle mentById('text' ).innerHTML = "<P>" + T[3] + "</P>";
      }
      }

      is IIRC equivalent, and could be a bit quicker.

      But I've seen your second post in this thread.

      document.getEle mentById('text' ).innerHTML = "";
      for (i = 0; i < txtdef.length; T = txtdef[i++])
      if ( xx == T[0] && yy == T[1] && level == T[2] )
      document.getEle mentById('text' ).innerHTML = "<P>" + T[3] + "</P>";

      For a further improvement, note that only the last assignment has
      effect.

      document.getEle mentById('text' ).innerHTML = "";
      for (i = txtdef.length; i >=0 ; T = txtdef[--i])
      if ( xx == T[0] && yy == T[1] && level == T[2] ) {
      document.getEle mentById('text' ).innerHTML = "<P>" + T[3] + "</P>" ;
      break }

      S = "" ;
      for (i = txtdef.length; i > 0 ; T = txtdef[--i])
      if ( xx == T[0] && yy == T[1] && level == T[2] ) {
      S = "<P>" + T[3] + "</P>" ;
      break }
      document.getEle mentById('text' ).innerHTML = S ;



      All untested.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

      Comment

      • Fabian

        #4
        Re: noticeable delay in processing a loop

        Dr John Stockton hu kiteb:
        [color=blue]
        > document.getEle mentById('text' ).innerHTML = "";
        > for (i = 0; i < txtdef.length; i++) {
        > T = txtdef[i]
        > if ( xx == T[0] && yy == T[1] && level == T[2] ) {[/color]

        Testing all these if statements at the same time is faster than testing
        them in series? I would be very interested in a definitive answer to
        this one.
        [color=blue]
        > S = "" ;
        > for (i = txtdef.length; i > 0 ; T = txtdef[--i])
        > if ( xx == T[0] && yy == T[1] && level == T[2] ) {
        > S = "<P>" + T[3] + "</P>" ;
        > break }
        > document.getEle mentById('text' ).innerHTML = S ;
        >
        >
        >
        > All untested.[/color]

        Tested, and working quite happily. Thank you. Improvements are slight
        compared to when I removed the text reflow situation, but still useful.
        The overall javascript throws around so many images that any little
        speed increase is useful.


        --
        --
        Fabian
        Visit my website often and for long periods!


        Comment

        • Dr John Stockton

          #5
          Re: noticeable delay in processing a loop

          JRS: In article <bqvais$25pgq4$ 1@ID-174912.news.uni-berlin.de>, seen in
          news:comp.lang. javascript, Fabian <lajzar@hotmail .com> posted at Sun, 7
          Dec 2003 22:43:28 :-[color=blue]
          >Dr John Stockton hu kiteb:
          >[color=green]
          >> document.getEle mentById('text' ).innerHTML = "";
          >> for (i = 0; i < txtdef.length; i++) {
          >> T = txtdef[i]
          >> if ( xx == T[0] && yy == T[1] && level == T[2] ) {[/color]
          >
          >Testing all these if statements at the same time is faster than testing
          >them in series? I would be very interested in a definitive answer to
          >this one.[/color]

          In each case, the testing can stop once the result is known. In a
          compiled language that permits partial boolean evaluation, the above
          approach should certainly be quicker. In javascript, it might or might
          not be. Certainly, if xx!=T[0] there is no need to test further &&
          items.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

          Comment

          Working...