Skip to content

Instantly share code, notes, and snippets.

@abozhilov
Created February 5, 2022 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abozhilov/d99acddb9a99f127a59c13cba515ad37 to your computer and use it in GitHub Desktop.
Save abozhilov/d99acddb9a99f127a59c13cba515ad37 to your computer and use it in GitHub Desktop.
Cached Intl.DateTimeFormat
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