[Perf] Message List: Avoid toLocaleTimeString because of bad performance

This commit is contained in:
Alexander Zinchuk 2021-11-09 21:21:33 +03:00
parent 5c3a530003
commit 842e86cd4c
2 changed files with 9 additions and 7 deletions

View File

@ -409,7 +409,7 @@ const Composer: FC<OwnProps & StateProps & DispatchProps> = ({
closeEmojiTooltip();
if (IS_SINGLE_COLUMN_LAYOUT) {
// @perf
// @optimization
setTimeout(() => closeSymbolMenu(), SENDING_ANIMATION_DURATION);
} else {
closeSymbolMenu();

View File

@ -31,17 +31,19 @@ function toIsoString(date: Date) {
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
}
// @optimization `toLocaleTimeString` is avoided because of bad performance
export function formatTime(datetime: number | Date, lang: LangFn) {
const date = typeof datetime === 'number' ? new Date(datetime) : datetime;
const timeFormat = lang.timeFormat || '24h';
const time = date.toLocaleTimeString(lang.code, {
hour12: timeFormat === '12h',
hour: timeFormat === '12h' ? 'numeric' : '2-digit',
minute: '2-digit',
});
let hours = date.getHours();
let marker = '';
if (timeFormat === '12h') {
marker = hours >= 12 ? ' PM' : ' AM';
hours %= 12;
}
return timeFormat === '12h' ? time.replace(/^0:/, '12:') : time.replace('24:', '00:');
return `${String(hours).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}${marker}`;
}
export function formatPastTimeShort(lang: LangFn, datetime: number | Date) {