Last active
December 4, 2023 11:25
-
-
Save NikiSkaarup/d18d3e7de08887b698a1fa671a8bf456 to your computer and use it in GitHub Desktop.
performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Diy Date | |
/** | |
* @param {Date} date | |
*/ | |
function getDiyDate(date) { | |
const year = date.getFullYear(); | |
const month = date.getMonth() + 1; | |
const monthString = month < 10 ? `0${month}` : month; | |
const day = date.getDate(); | |
const dayString = day < 10 ? `0${day}` : day; | |
const hours = date.getHours(); | |
const minutes = date.getMinutes(); | |
const hoursShort = hours % 12; | |
const hoursString = hoursShort < 10 ? `0${hoursShort}` : hoursShort; | |
const minutesString = minutes < 10 ? `0${minutes}` : minutes; | |
const timeOfDay = hours < 12 ? "AM" : "PM"; | |
return `${monthString}/${dayString}/${year}, ${hoursString}:${minutesString} ${timeOfDay}`; | |
} | |
// /Diy Date | |
// Internationalization formatted date | |
const intlFormatter = new Intl.DateTimeFormat("en-US", { | |
day: "2-digit", | |
month: "2-digit", | |
year: "numeric", | |
hour: "2-digit", | |
minute: "2-digit", | |
}); | |
// /Internationalization formatted date | |
const iter = 100000; | |
// warmup | |
console.log("warmup starting"); | |
for (let i = iter; i--; ) { | |
getDiyDate(new Date("2023-11-21T00:00:00.000Z")); | |
} | |
for (let i = iter; i--; ) { | |
intlFormatter.format(Date.parse("2023-11-21T00:00:00.000Z")); | |
} | |
console.log("warmup done"); | |
// /warmup | |
// actual test | |
const startDiy = performance.now(); | |
const diyDate = getDiyDate(new Date("2023-11-21T00:00:00.000Z")); | |
const endDiy = performance.now(); | |
const startIntl = performance.now(); | |
const intlDate = intlFormatter.format(Date.parse("2023-11-21T00:00:00.000Z")); | |
const endIntl = performance.now(); | |
// /actual test | |
// results | |
console.log( | |
`getDiyDates took ${(endDiy - startDiy).toFixed(20)} milliseconds,`, | |
diyDate | |
); | |
console.log( | |
`getIntlDates took ${(endIntl - startIntl).toFixed(20)} milliseconds,`, | |
intlDate | |
); | |
const date = new Date("2023-11-21T00:00:00.000Z"); | |
const iter2 = 100000; | |
// warmup | |
console.log("warmup starting"); | |
for (let i = iter2; i--; ) { | |
getDiyDate(date); | |
} | |
for (let i = iter2; i--; ) { | |
intlFormatter.format(date); | |
} | |
console.log("warmup done"); | |
// /warmup | |
// actual test | |
const startDiy2 = performance.now(); | |
const diyDate2 = getDiyDate(date); | |
const endDiy2 = performance.now(); | |
const startIntl2 = performance.now(); | |
const intlDate2 = intlFormatter.format(date); | |
const endIntl2 = performance.now(); | |
// /actual test | |
// results | |
console.log( | |
`getDiyDates took ${(endDiy2 - startDiy2).toFixed(20)} milliseconds,`, | |
diyDate2 | |
); | |
console.log( | |
`getIntlDates took ${(endIntl2 - startIntl2).toFixed(20)} milliseconds,`, | |
intlDate2 | |
); | |
/* | |
node ./index.js | |
warmup starting | |
warmup done | |
getDiyDates took 0.01456000003963708878 milliseconds, 11/21/2023, 01:00 AM | |
getIntlDates took 0.00782100018113851547 milliseconds, 11/21/2023, 01:00 AM | |
warmup starting | |
warmup done | |
getDiyDates took 0.00626999977976083755 milliseconds, 11/21/2023, 01:00 AM | |
getIntlDates took 0.00490000005811452866 milliseconds, 11/21/2023, 01:00 AM | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment