Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Looking for more performance
Message
From
14/04/2016 06:04:31
 
 
To
13/04/2016 21:25:47
General information
Forum:
Javascript
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01634763
Message ID:
01634778
Views:
57
Likes (1)
>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?

Seems to me you are doing some calculations unnecessarily (e.g current date every time, hours, minutes and seconds when only day is of interest). Also I think the calculations themselves could be simplified. This looks shorter:
function SinceWhen(receivedDate, currentDate, language) {
            const ONE_SECOND = 1000;
            const ONE_MINUTE = ONE_SECOND * 60;
            const ONE_HOUR = ONE_MINUTE * 60;
            const ONE_DAY = ONE_HOUR * 24;

            const SECOND = "second";
            const MINUTE = "minute";
            const HOUR = "hour";
            const DAY = "day";
            const NOW = "Just now";
            const YESTERDAY = "Yesterday";

            var retstring = "";

            var elapsedDays =  parseInt((currentDate - receivedDate) / ONE_DAY,10);
            if (elapsedDays > 0) {
                if (elapsedDays === 1){
                    retstring = YESTERDAY;
                } else {
                    retString = getString(DAY, elapsedSeconds);
                }
            } else {
                var elapsedHours = parseInt( (currentDate - receivedDate) / ONE_HOUR,10);
                if (elapsedHours > 0) {
                    retString = getString(HOUR, elapsedSeconds);
                } else {
                    var elapsedMinutes = parseInt((currentDate - receivedDate) / ONE_MINUTE, 10);
                    if (elapsedMinutes > 0) {
                        retString = getString(MINUTE, elapsedSeconds);
                    } else {
                        var elapsedSeconds = parseInt((currentDate - receivedDate) / ONE_SECOND, 10);
                        if (elapsedSeconds < 10) {
                            retstring = NOW
                        } else {
                            retString = getString(SECOND, elapsedSeconds);
                        }
                    }
                }
            }
            return retstring;
        }

        function getString(span, length) {
            if (length === 1) {
                return "One " + span + " ago";
            } else {
                return length + " " + span + "s ago";
            }
        }
Not properly tested :-} Not dealing with the language issue (which could be handled in the getString() method) But I'm not sure it would improve performance - manipulating the DOM is probably taking most of the time.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform