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

Another intersting approach, thanks for sharing it.
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform