fix(frontend): improve performance by reducing array constructions

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-06-26 00:09:07 +02:00
parent 28f04f461c
commit ced4cd953c
6 changed files with 26 additions and 24 deletions

View file

@ -35,9 +35,11 @@ export class YTextSyncViewPlugin implements PluginValue {
const [changes] = event.delta.reduce(
([changes, position], delta) => {
if (delta.insert !== undefined && typeof delta.insert === 'string') {
return [[...changes, { from: position, to: position, insert: delta.insert }], position]
changes.push({ from: position, to: position, insert: delta.insert })
return [changes, position]
} else if (delta.delete !== undefined) {
return [[...changes, { from: position, to: position + delta.delete, insert: '' }], position + delta.delete]
changes.push({ from: position, to: position + delta.delete, insert: '' })
return [changes, position + delta.delete]
} else if (delta.retain !== undefined) {
return [changes, position + delta.retain]
} else {

View file

@ -11,6 +11,7 @@ import { linter } from '@codemirror/lint'
import type { Extension } from '@codemirror/state'
import type { EditorView } from '@codemirror/view'
import { useMemo } from 'react'
import { useTranslation } from 'react-i18next'
/**
* The Linter interface.
@ -32,11 +33,16 @@ const createLinterExtension = () =>
/**
* Creates a codemirror linter that runs all markdown extension linters.
* Due to a bug in codemirror that breaks the "fix" buttons when switching themes, the extension is recreated if the app switches between dark and light mode.
* To update translations the t function is also included.
*
* @return The build codemirror linter extension
*/
export const useLinter = (): Extension => {
const darkModeActivated = useDarkModeState()
const { t } = useTranslation()
return useMemo(() => (darkModeActivated ? createLinterExtension() : createLinterExtension()), [darkModeActivated])
return useMemo(
() => (darkModeActivated && !!t ? createLinterExtension() : createLinterExtension()),
[darkModeActivated, t]
)
}

View file

@ -30,19 +30,15 @@ export class SingleLineRegexLinter implements Linter {
) {}
lint(view: EditorView): Diagnostic[] {
return view.state.doc
.toString()
.split('\n')
.reduce(
(state, line, lineIndex, lines) => [
...state,
{
line,
startIndex: lineIndex === 0 ? 0 : state[lineIndex - 1].startIndex + lines[lineIndex - 1].length + 1
} as LineWithStartIndex
],
[] as LineWithStartIndex[]
)
const lines = view.state.doc.toString().split('\n')
return lines
.reduce((state, line, lineIndex, lines) => {
state[lineIndex] = {
line,
startIndex: lineIndex === 0 ? 0 : state[lineIndex - 1].startIndex + lines[lineIndex - 1].length + 1
} as LineWithStartIndex
return state
}, new Array<LineWithStartIndex>(lines.length))
.map(({ line, startIndex }) => ({
lineStartIndex: startIndex,
regexResult: this.regex.exec(line)

View file

@ -42,7 +42,8 @@ const processCommentNode = (node: DataNode): void => {
return
}
for (const dataAttribute of [...regexResult[2].matchAll(dataAttributesSyntax)]) {
const matches = [...regexResult[2].matchAll(dataAttributesSyntax)]
for (const dataAttribute of matches) {
const attributeName = dataAttribute[1]
const attributeValue = dataAttribute[2] ?? dataAttribute[3]
if (attributeValue) {

View file

@ -63,10 +63,7 @@ export class LineContentToLineIdMapper {
LineContentToLineIdMapper.changeIsNotChangingLines(change) ||
LineContentToLineIdMapper.changeIsAddingLines(change)
)
.reduce(
(previousLineKeys, currentChange) => [...previousLineKeys, ...this.convertChangeToLinesWithIds(currentChange)],
[] as LineWithId[]
)
.flatMap((currentChange) => this.convertChangeToLinesWithIds(currentChange))
}
/**

View file

@ -12,7 +12,7 @@
*/
export const calculateLineStartIndexes = (markdownContentLines: string[]): number[] => {
return markdownContentLines.reduce((state, line, lineIndex, lines) => {
const lastIndex = lineIndex === 0 ? 0 : state[lineIndex - 1] + lines[lineIndex - 1].length + 1
return [...state, lastIndex]
}, [] as number[])
state[lineIndex] = lineIndex === 0 ? 0 : state[lineIndex - 1] + lines[lineIndex - 1].length + 1
return state
}, new Array<number>(markdownContentLines.length))
}