[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');
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);

View File

@ -72,11 +72,9 @@ const SettingsFoldersChatFilters: FC<OwnProps & StateProps & DispatchProps> = ({
}
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<OwnProps & StateProps & DispatchProps> = ({
}
}, [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 <Loading />;

View File

@ -45,34 +45,29 @@ const ForwardPicker: FC<OwnProps & StateProps & DispatchProps> = ({
const filterRef = useRef<HTMLInputElement>(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) => {

View File

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