[Perf] Remove some redundant array spreads

This commit is contained in:
Alexander Zinchuk 2021-12-14 22:40:47 +04:00
parent d4d6166ede
commit 0aa815066e
4 changed files with 45 additions and 47 deletions

View File

@ -70,12 +70,14 @@ function escapeHtml(textParts: TextPart[]): TextPart[] {
const divEl = document.createElement('div'); const divEl = document.createElement('div');
return textParts.reduce((result, part) => { return textParts.reduce((result, part) => {
if (typeof part !== 'string') { if (typeof part !== 'string') {
return [...result, part]; result.push(part);
return result;
} }
divEl.innerText = part; divEl.innerText = part;
result.push(divEl.innerHTML);
return [...result, divEl.innerHTML]; return result;
}, [] as TextPart[]); }, [] as TextPart[]);
} }
@ -86,7 +88,8 @@ function replaceEmojis(textParts: TextPart[], size: 'big' | 'small', type: 'jsx'
return textParts.reduce((result, part) => { return textParts.reduce((result, part) => {
if (typeof part !== 'string') { if (typeof part !== 'string') {
return [...result, part]; result.push(part);
return result;
} }
part = fixNonStandardEmoji(part); part = fixNonStandardEmoji(part);
@ -129,12 +132,13 @@ function replaceEmojis(textParts: TextPart[], size: 'big' | 'small', type: 'jsx'
} }
function addLineBreaks(textParts: TextPart[], type: 'jsx' | 'html'): TextPart[] { function addLineBreaks(textParts: TextPart[], type: 'jsx' | 'html'): TextPart[] {
return textParts.reduce((result, part) => { return textParts.reduce((result: TextPart[], part) => {
if (typeof part !== 'string') { if (typeof part !== 'string') {
return [...result, part]; result.push(part);
return result;
} }
return [...result, ...part const splittenParts = part
.split(/\r\n|\r|\n/g) .split(/\r\n|\r|\n/g)
.reduce((parts: TextPart[], line: string, i, source) => { .reduce((parts: TextPart[], line: string, i, source) => {
// This adds non-breaking space if line was indented with spaces, to preserve the indentation // 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; return parts;
}, [])]; }, []);
}, [] as TextPart[]);
return [...result, ...splittenParts];
}, []);
} }
function addHighlight(textParts: TextPart[], highlight: string | undefined): TextPart[] { function addHighlight(textParts: TextPart[], highlight: string | undefined): TextPart[] {
return textParts.reduce((result, part) => { return textParts.reduce((result, part) => {
if (typeof part !== 'string' || !highlight) { if (typeof part !== 'string' || !highlight) {
return [...result, part]; result.push(part);
return result;
} }
const lowerCaseText = part.toLowerCase(); const lowerCaseText = part.toLowerCase();
const queryPosition = lowerCaseText.indexOf(highlight.toLowerCase()); const queryPosition = lowerCaseText.indexOf(highlight.toLowerCase());
const nextSymbol = lowerCaseText[queryPosition + highlight.length]; const nextSymbol = lowerCaseText[queryPosition + highlight.length];
if (queryPosition < 0 || (nextSymbol && nextSymbol.match(RE_LETTER_OR_DIGIT))) { if (queryPosition < 0 || (nextSymbol && nextSymbol.match(RE_LETTER_OR_DIGIT))) {
return [...result, part]; result.push(part);
return result;
} }
const newParts: TextPart[] = []; const newParts: TextPart[] = [];
@ -184,12 +192,14 @@ const RE_LINK = new RegExp(`${RE_LINK_TEMPLATE}|${RE_MENTION_TEMPLATE}`, 'ig');
function addLinks(textParts: TextPart[]): TextPart[] { function addLinks(textParts: TextPart[]): TextPart[] {
return textParts.reduce((result, part) => { return textParts.reduce((result, part) => {
if (typeof part !== 'string') { if (typeof part !== 'string') {
return [...result, part]; result.push(part);
return result;
} }
const links = part.match(RE_LINK); const links = part.match(RE_LINK);
if (!links || !links.length) { if (!links || !links.length) {
return [...result, part]; result.push(part);
return result;
} }
const content: TextPart[] = []; const content: TextPart[] = [];
@ -226,7 +236,8 @@ function addLinks(textParts: TextPart[]): TextPart[] {
function replaceSimpleMarkdown(textParts: TextPart[], type: 'jsx' | 'html'): TextPart[] { function replaceSimpleMarkdown(textParts: TextPart[], type: 'jsx' | 'html'): TextPart[] {
return textParts.reduce((result, part) => { return textParts.reduce((result, part) => {
if (typeof part !== 'string') { if (typeof part !== 'string') {
return [...result, part]; result.push(part);
return result;
} }
const parts = part.split(SIMPLE_MARKDOWN_REGEX); const parts = part.split(SIMPLE_MARKDOWN_REGEX);

View File

@ -72,11 +72,9 @@ const SettingsFoldersChatFilters: FC<OwnProps & StateProps & DispatchProps> = ({
} }
return [ return [
...(activeChatArrays ...(activeChatArrays?.pinnedChats || []),
? [...activeChatArrays.pinnedChats, ...activeChatArrays.otherChats] ...(activeChatArrays?.otherChats || []),
: [] ...(archivedChatArrays?.otherChats || []),
),
...(archivedChatArrays ? archivedChatArrays.otherChats : []),
]; ];
}, [chatsById, listIds, orderedPinnedIds, archivedListIds, archivedPinnedIds]); }, [chatsById, listIds, orderedPinnedIds, archivedListIds, archivedPinnedIds]);
@ -140,8 +138,10 @@ const SettingsFoldersChatFilters: FC<OwnProps & StateProps & DispatchProps> = ({
} }
}, [mode, selectedChatIds, dispatch]); }, [mode, selectedChatIds, dispatch]);
useHistoryBack(isActive, onReset, onScreenSelect, useHistoryBack(
mode === 'included' ? SettingsScreens.FoldersIncludedChats : SettingsScreens.FoldersExcludedChats); isActive, onReset, onScreenSelect,
mode === 'included' ? SettingsScreens.FoldersIncludedChats : SettingsScreens.FoldersExcludedChats,
);
if (!displayedIds) { if (!displayedIds) {
return <Loading />; return <Loading />;

View File

@ -45,34 +45,29 @@ const ForwardPicker: FC<OwnProps & StateProps & DispatchProps> = ({
const filterRef = useRef<HTMLInputElement>(null); const filterRef = useRef<HTMLInputElement>(null);
const chatIds = useMemo(() => { const chatIds = useMemo(() => {
const listIds = [ const listIds = [...(activeListIds || []), ...(archivedListIds || [])];
...(activeListIds || []),
...(archivedListIds || []),
];
let priorityIds = pinnedIds || []; let priorityIds = pinnedIds || [];
if (currentUserId) { if (currentUserId) {
priorityIds = unique([currentUserId, ...priorityIds]); priorityIds = unique([currentUserId, ...priorityIds]);
} }
return sortChatIds([ return sortChatIds(listIds.filter((id) => {
...listIds.filter((id) => { const chat = chatsById[id];
const chat = chatsById[id]; if (!chat) {
if (!chat) { return true;
return true; }
}
if (!getCanPostInChat(chat, MAIN_THREAD_ID)) { if (!getCanPostInChat(chat, MAIN_THREAD_ID)) {
return false; return false;
} }
if (!filter) { if (!filter) {
return true; return true;
} }
return searchWords(getChatTitle(lang, chatsById[id], undefined, id === currentUserId), filter); return searchWords(getChatTitle(lang, chatsById[id], undefined, id === currentUserId), filter);
}), }), chatsById, undefined, priorityIds);
], chatsById, undefined, priorityIds);
}, [activeListIds, archivedListIds, chatsById, currentUserId, filter, lang, pinnedIds]); }, [activeListIds, archivedListIds, chatsById, currentUserId, filter, lang, pinnedIds]);
const handleSelectUser = useCallback((userId: string) => { const handleSelectUser = useCallback((userId: string) => {

View File

@ -139,14 +139,6 @@ export function cloneDeep<T>(value: T): T {
}, {} as 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<T>(array: Array<T>, predicate: (value: T, index: number, obj: T[]) => boolean): T | undefined { export function findLast<T>(array: Array<T>, predicate: (value: T, index: number, obj: T[]) => boolean): T | undefined {
let cursor = array.length; let cursor = array.length;