perf: optimized humanTime

humanTime function improved with a 15% increase in performance.
This commit is contained in:
Alex 2022-05-30 17:56:23 +12:00
parent d7952575ad
commit 1817a125c0
1 changed files with 11 additions and 11 deletions

View File

@ -134,21 +134,21 @@ const module = {
humanTime(seconds = 0) {
seconds = Math.floor(seconds); // Not doing this seems to break the calculation
let levels = [
Math.floor(seconds / 31536000), //Years
Math.floor((seconds % 31536000) / 86400), //Days
Math.floor(((seconds % 31536000) % 86400) / 3600), //Hours
Math.floor(seconds / 31536000) || null, //Years
Math.floor((seconds % 31536000) / 86400) || null, //Days
Math.floor(((seconds % 31536000) % 86400) / 3600) || null, //Hours
Math.floor((((seconds % 31536000) % 86400) % 3600) / 60), //Minutes
(((seconds % 31536000) % 86400) % 3600) % 60, //Seconds
Math.floor((((seconds % 31536000) % 86400) % 3600) % 60), //Seconds
];
levels = levels.filter((level) => level !== null);
let returntext = new String();
for (const i in levels) {
const num = levels[i].toString().length == 1 ? "0" + levels[i] : levels[i]; // If Number Is Single Digit, Add 0 In Front
returntext += ":" + num;
for (let i = 1; i < levels.length; i++) {
levels[i] = levels[i].toString().padStart(2, "0");
}
while (returntext.startsWith(":00")) { returntext = returntext.substring(3); } // Remove Prepending 0s (eg. 00:00:00:01:00)
if (returntext.startsWith(":0")) { returntext = returntext.substring(2); } else { returntext = returntext.substring(1); } // Prevent Time Starting With 0 (eg. 01:00)
// join the array into a string with : as a separator
const returntext = levels.join(":");
console.log("Human Time:", returntext);
return returntext;
},