Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Looking for more performance
Message
De
16/04/2016 07:05:19
 
 
À
13/04/2016 21:25:47
Information générale
Forum:
Javascript
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01634763
Message ID:
01634874
Vues:
42
>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.
>
>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?

Saturday morning breakfast code, rewritten as follows (the other posters in the thread will recognize the various inputs you got):
function NewsFeedRefresh() {

    window.clearTimeout(oTimerNewsFeed);

    var loDivNewsFeed = document.getElementById("DivNewsFeed");

    if (loDivNewsFeed) {

        var lnLength = oNewsFeed.length;

        for (var lnCounter = 0; lnCounter < lnLength; lnCounter++) {

            var loID = document.getElementById("LX" + oNewsFeed[lnCounter][1] + "Time");
            
            if (loID)
                loID.innerHTML = SinceWhen(oNewsFeed[lnCounter][2], oNewsFeed[lnCounter][3]);
        }

        oTimerNewsFeed = setTimeout("NewsFeedRefresh()", 60000)
    }
}

var timeMessage = [[["Yesterday","%d days ago"],
                    ["One hour ago", "%d hours ago"],
                    ["One minute ago", "%d minutes ago"],
                    ["Just now", "%d seconds ago"]],
                   [["Hier", "Il y a %d jours"],
                    ["Il y a une heure", "Il y a %d heures"],
                    ["Il y a une minute", "Il y a %d minutes"],
                    ["Maintenant", "Il y a %d secondes"]]];

function SinceWhen (tdDate, tnNoLanguage) {

    var lnSecond = Math.floor(((new Date()).getTime() - tdDate.getTime()) / 1000);

    if (lnSecond >= 86400){
        var lnUnits = Math.floor(lnSecond / 86400);
        var lnIndex = 0;
        var lnNoUnits = 1;        
    } else if (lnSecond >= 3600){
        var lnUnits = Math.floor(lnSecond / 3600);
        var lnIndex = 1;
        var lnNoUnits = 1;
    } else if (lnSecond >= 60){
        var lnUnits = Math.floor(lnSecond / 60);
        var lnIndex = 2;
        var lnNoUnits = 1;
    } else {
        var lnUnits = lnSecond;
        var lnIndex = 3;
        var lnNoUnits = 10;
    }

    return timeMessage[tnNoLanguage - 1][lnIndex][lnUnits > lnNoUnits ? 1 : 0].replace(/%d/,lnUnits);
}
One obvious optimization is to call for variable instantiation and maths calculations only when / if needed.

The other is to get rid of parseInt() calls which, although ending with the result you're looking for, does it in a very inefficient manner, since it requires the resolution of the expression, its conversion to string, and then its conversion back to integer.
----------------------------------
António Tavares Lopes
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform