-
-
Save abozhilov/d99acddb9a99f127a59c13cba515ad37 to your computer and use it in GitHub Desktop.
Cached Intl.DateTimeFormat
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
const formatter = new Intl.DateTimeFormat('en-US', { | |
year: 'numeric', | |
month: '2-digit', | |
day: '2-digit', | |
hour: '2-digit', | |
minute: '2-digit' | |
}); | |
function format(date: Date) { | |
const mapParts = Object.create(null); | |
const parts = formatter.formatToParts(date); | |
for (const {type, value} of parts) { | |
mapParts[type] = value; | |
} | |
return `${mapParts.year}-${mapParts.month}-${mapParts.day} ${mapParts.hour}:${mapParts.minute}`; | |
} | |
const date = new Date(); | |
const start = new Date(); | |
for (let i = 0; i < 100000; i++) { | |
format(date); | |
} | |
const end = new Date(); | |
console.log(format(date)); | |
console.log(end.getTime() - start.getTime()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment