Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Looking for more performance
Message
From
14/04/2016 05:01:43
 
 
To
13/04/2016 21:25:47
General information
Forum:
Javascript
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01634763
Message ID:
01634777
Views:
59
Michel,

in dumb interpreters unnecessary operations will slow down - so I took some out, benefit doubtful, as a smart compiler (Google V8 is a compiler)
should do that optimization as well.

This is probably only lessening the pain! It might very well be that setting lots of innerHTML is very slow - read up on the concepts of React, Mithril or Vue.JS how they set up dependencies or some employ shadow DOM to speed up browser GUI speed - although that is an area I am a lot less firm in compared to vfp or DB optimization.

Probably Craig is correct at least insofar as perhaps a more "architectural" approach is needed and not micro-optimizations like mine.
If everything else fails, change concept and show time of next refresh for each item in the for loop - then the task is reduced to comparing the time stamps and most of "work" currently done is skipped, including setting new innerHTML. Sometimes it is smarter to avoid "doing it the hard way" ;-))
See inline changes to your code. UNTESTED!!!


>I have a function that is called which is responsible to update various DIVs on a page. The function NewsFeedRefresh() is called on interval and has the responsibility to update the time of various items on the page.
>
>This is the code of that function as well as the SinceWhen() function:
>
>
>function NewsFeedRefresh()
>{
>   window.clearTimeout(oTimerNewsFeed)
>   var lcID=""
>   var ldDate=null
>   var lnLength=oNewsFeed.length
>   var lnNoLanguage=0
>   var loID=null
>   loDivNewsFeed=document.getElementById("DivNewsFeed")
>   if (loDivNewsFeed)
>   {
>   }
>   else
>   {
>      return true
>   }
>   if (lnLength==0)
>   {
>      oTimerNewsFeed=setTimeout("NewsFeedRefresh()",60000)
>      return true
>   }
>   for (lnCounter=0; lnCounter<lnLength; lnCounter++)
>   {
>      loID=document.getElementById("LX"+oNewsFeed[lnCounter][1]+"Time")
>      if (loID)
>      {
>         loID.innerHTML=SinceWhen(oNewsFeed[lnCounter][2],  oNewsFeed[lnCounter][3])
>      }
>   }
>   oTimerNewsFeed=setTimeout("NewsFeedRefresh()",60000)
>}
>
>function SinceWhen(tdDate,tnNoLanguage)
>{
// the benefit of manually cutting out of unnecessary operations will depend on the optimization strategy of Javascript Compiler/Interpreter
// V8 might do those optimizations by itself
>   var ldDateNow=new Date()
>   var lnMillisecond=ldDateNow.getTime()-tdDate.getTime()
>   var lnSecond=parseInt(lnMillisecond/1000)
>   var lnDay=parseInt(lnSecond/86400)
>   var lnRemaining=lnSecond-(lnDay*86400)
>   var lnHour=parseInt(lnRemaining/3600)
>   lnRemaining=lnRemaining-(lnHour*3600)
>   var lnMinute=parseInt(lnRemaining/60)
>   lnSecond=lnRemaining-(lnMinute*60)

// predefining/-allocating all possible results in a array housed "more global" is more difficult as you embed a variable string sometimes
// as array index would have to be enforced via min() to 1 for cases like lnDay>0, speed gain of such an approach even more doubtful
// might also be dependant on Javascript engine, so test in all browsers !
// return SinceWhenReturnOptions[][]
// I would at least try out speed of ternary operator "?" compared to the most inner if-assignments

>   var lcTime=""
>   switch (tnNoLanguage)
>   {
>      case 1:
>         if (lnDay>0)
>         {
>            if (lnDay==1)
>            {
>               lcTime="Yesterday"
>            }
>            else
>            {
>               lcTime=lnDay+" days ago"
>            }
>         }
>         else
>         {
>            if (lnHour>0)
>            {
>               if (lnHour==1)
>               {
>                  lcTime="One hour ago"
>               }
>               else
>               {
>                  lcTime=lnHour+" hours ago"
>               }
>            }
>            else
>            {
>               if (lnMinute>0)
>               {
>                  if (lnMinute==1)
>                  {
>                     lcTime="One minute ago"
>                  }
>                  else
>                  {
>                     lcTime=lnMinute+" minutes ago"
>                  }
>               }
>               else
>               {
>                  if (lnSecond<10)
>                  {
>                     lcTime="Just now"
>                  }
>                  else
>                  {
>                     lcTime=lnSecond+" seconds ago"
>                  }
>               }
>            }
>         }
>         break
>      case 2:
>         if (lnDay>0)
>         {
>            if (lnDay==1)
>            {
>               lcTime="Hier"
>            }
>            else
>            {
>               lcTime="Il y a "+lnDay+" jours"
>            }
>         }
>         else
>         {
>            if (lnHour>0)
>            {
>               if (lnHour==1)
>               {
>                  lcTime="Il y a une heure"
>               }
>               else
>               {
>                  lcTime="Il y a "+lnHour+" heures"
>               }
>            }
>            else
>            {
>               if (lnMinute>0)
>               {
>                  if (lnMinute==1)
>                  {
>                     lcTime="Il y a une minute"
>                  }
>                  else
>                  {
>                     lcTime="Il y a "+lnMinute+" minutes"
>                  }
>               }
>               else
>               {
>                  if (lnSecond<10)
>                  {
>                     lcTime="Maintenant"
>                  }
>                  else
>                  {
>                     lcTime="Il y a "+lnSecond+" secondes"
>                  }
>               }
>            }
>         }
>         break
>   }
>   return lcTime
>}
>
>
>Something is very slow here if I have about 200 items on the page. I would have expected that to slow around 40,000. Anyone would have an idea on how to optimize this logic to avoid this javascript latency?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform