From 0aa815066e087d9dfba0701e751339f97c76a886 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 14 Dec 2021 22:40:47 +0400 Subject: [PATCH] [Perf] Remove some redundant array spreads --- src/components/common/helpers/renderText.tsx | 37 ++++++++++++------- .../folders/SettingsFoldersChatFilters.tsx | 14 +++---- src/components/main/ForwardPicker.tsx | 33 +++++++---------- src/util/iteratees.ts | 8 ---- 4 files changed, 45 insertions(+), 47 deletions(-) diff --git a/src/components/common/helpers/renderText.tsx b/src/components/common/helpers/renderText.tsx index 248b1f7e5..e99a41088 100644 --- a/src/components/common/helpers/renderText.tsx +++ b/src/components/common/helpers/renderText.tsx @@ -70,12 +70,14 @@ function escapeHtml(textParts: TextPart[]): TextPart[] { const divEl = document.createElement('div'); return textParts.reduce((result, part) => { if (typeof part !== 'string') { - return [...result, part]; + result.push(part); + return result; } divEl.innerText = part; + result.push(divEl.innerHTML); - return [...result, divEl.innerHTML]; + return result; }, [] as TextPart[]); } @@ -86,7 +88,8 @@ function replaceEmojis(textParts: TextPart[], size: 'big' | 'small', type: 'jsx' return textParts.reduce((result, part) => { if (typeof part !== 'string') { - return [...result, part]; + result.push(part); + return result; } part = fixNonStandardEmoji(part); @@ -129,12 +132,13 @@ function replaceEmojis(textParts: TextPart[], size: 'big' | 'small', type: 'jsx' } function addLineBreaks(textParts: TextPart[], type: 'jsx' | 'html'): TextPart[] { - return textParts.reduce((result, part) => { + return textParts.reduce((result: TextPart[], part) => { if (typeof part !== 'string') { - return [...result, part]; + result.push(part); + return result; } - return [...result, ...part + const splittenParts = part .split(/\r\n|\r|\n/g) .reduce((parts: TextPart[], line: string, i, source) => { // This adds non-breaking space if line was indented with spaces, to preserve the indentation @@ -149,21 +153,25 @@ function addLineBreaks(textParts: TextPart[], type: 'jsx' | 'html'): TextPart[] } return parts; - }, [])]; - }, [] as TextPart[]); + }, []); + + return [...result, ...splittenParts]; + }, []); } function addHighlight(textParts: TextPart[], highlight: string | undefined): TextPart[] { return textParts.reduce((result, part) => { if (typeof part !== 'string' || !highlight) { - return [...result, part]; + result.push(part); + return result; } const lowerCaseText = part.toLowerCase(); const queryPosition = lowerCaseText.indexOf(highlight.toLowerCase()); const nextSymbol = lowerCaseText[queryPosition + highlight.length]; if (queryPosition < 0 || (nextSymbol && nextSymbol.match(RE_LETTER_OR_DIGIT))) { - return [...result, part]; + result.push(part); + return result; } const newParts: TextPart[] = []; @@ -184,12 +192,14 @@ const RE_LINK = new RegExp(`${RE_LINK_TEMPLATE}|${RE_MENTION_TEMPLATE}`, 'ig'); function addLinks(textParts: TextPart[]): TextPart[] { return textParts.reduce((result, part) => { if (typeof part !== 'string') { - return [...result, part]; + result.push(part); + return result; } const links = part.match(RE_LINK); if (!links || !links.length) { - return [...result, part]; + result.push(part); + return result; } const content: TextPart[] = []; @@ -226,7 +236,8 @@ function addLinks(textParts: TextPart[]): TextPart[] { function replaceSimpleMarkdown(textParts: TextPart[], type: 'jsx' | 'html'): TextPart[] { return textParts.reduce((result, part) => { if (typeof part !== 'string') { - return [...result, part]; + result.push(part); + return result; } const parts = part.split(SIMPLE_MARKDOWN_REGEX); diff --git a/src/components/left/settings/folders/SettingsFoldersChatFilters.tsx b/src/components/left/settings/folders/SettingsFoldersChatFilters.tsx index a49477c7f..addc9a7b1 100644 --- a/src/components/left/settings/folders/SettingsFoldersChatFilters.tsx +++ b/src/components/left/settings/folders/SettingsFoldersChatFilters.tsx @@ -72,11 +72,9 @@ const SettingsFoldersChatFilters: FC = ({ } return [ - ...(activeChatArrays - ? [...activeChatArrays.pinnedChats, ...activeChatArrays.otherChats] - : [] - ), - ...(archivedChatArrays ? archivedChatArrays.otherChats : []), + ...(activeChatArrays?.pinnedChats || []), + ...(activeChatArrays?.otherChats || []), + ...(archivedChatArrays?.otherChats || []), ]; }, [chatsById, listIds, orderedPinnedIds, archivedListIds, archivedPinnedIds]); @@ -140,8 +138,10 @@ const SettingsFoldersChatFilters: FC = ({ } }, [mode, selectedChatIds, dispatch]); - useHistoryBack(isActive, onReset, onScreenSelect, - mode === 'included' ? SettingsScreens.FoldersIncludedChats : SettingsScreens.FoldersExcludedChats); + useHistoryBack( + isActive, onReset, onScreenSelect, + mode === 'included' ? SettingsScreens.FoldersIncludedChats : SettingsScreens.FoldersExcludedChats, + ); if (!displayedIds) { return ; diff --git a/src/components/main/ForwardPicker.tsx b/src/components/main/ForwardPicker.tsx index 9b2784758..a8fa3d752 100644 --- a/src/components/main/ForwardPicker.tsx +++ b/src/components/main/ForwardPicker.tsx @@ -45,34 +45,29 @@ const ForwardPicker: FC = ({ const filterRef = useRef(null); const chatIds = useMemo(() => { - const listIds = [ - ...(activeListIds || []), - ...(archivedListIds || []), - ]; + const listIds = [...(activeListIds || []), ...(archivedListIds || [])]; let priorityIds = pinnedIds || []; if (currentUserId) { priorityIds = unique([currentUserId, ...priorityIds]); } - return sortChatIds([ - ...listIds.filter((id) => { - const chat = chatsById[id]; - if (!chat) { - return true; - } + return sortChatIds(listIds.filter((id) => { + const chat = chatsById[id]; + if (!chat) { + return true; + } - if (!getCanPostInChat(chat, MAIN_THREAD_ID)) { - return false; - } + if (!getCanPostInChat(chat, MAIN_THREAD_ID)) { + return false; + } - if (!filter) { - return true; - } + if (!filter) { + return true; + } - return searchWords(getChatTitle(lang, chatsById[id], undefined, id === currentUserId), filter); - }), - ], chatsById, undefined, priorityIds); + return searchWords(getChatTitle(lang, chatsById[id], undefined, id === currentUserId), filter); + }), chatsById, undefined, priorityIds); }, [activeListIds, archivedListIds, chatsById, currentUserId, filter, lang, pinnedIds]); const handleSelectUser = useCallback((userId: string) => { diff --git a/src/util/iteratees.ts b/src/util/iteratees.ts index 8c866035e..c2a6298c3 100644 --- a/src/util/iteratees.ts +++ b/src/util/iteratees.ts @@ -139,14 +139,6 @@ export function cloneDeep(value: T): T { }, {} as T); } -/** - * Returns the index of the last element in the array where predicate is true, and -1 otherwise. - * - * @param array The source array to search in - * @param predicate find calls predicate once for each element of the array, in descending - * order, until it finds one where predicate returns true. If such an element is found, - * findLastIndex immediately returns that element index. Otherwise, findLastIndex returns -1. - */ export function findLast(array: Array, predicate: (value: T, index: number, obj: T[]) => boolean): T | undefined { let cursor = array.length;