mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 20:21:58 +03:00
perf: optimize text search by eliminating array allocations
Replaced the expensive `.split("\n")` per-document string allocation
with a stateful, case-insensitive `RegExp.prototype.exec()` approach.
This allows the search loop to lazily advance and only compute line
boundaries for matched strings rather than eagerly allocating arrays
for the entire workspace on every keystroke.
Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
bede889ced
commit
bad171e1ce
1 changed files with 37 additions and 11 deletions
|
|
@ -44,6 +44,10 @@ export function parseResultQuery(
|
||||||
return { op: m[1], threshold: v };
|
return { op: m[1], threshold: v };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function escapeRegExp(string: string) {
|
||||||
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||||
|
}
|
||||||
|
|
||||||
export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] {
|
export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] {
|
||||||
const q = query.trim();
|
const q = query.trim();
|
||||||
if (!q) return [];
|
if (!q) return [];
|
||||||
|
|
@ -95,20 +99,42 @@ export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] {
|
||||||
}
|
}
|
||||||
return hits;
|
return hits;
|
||||||
}
|
}
|
||||||
const ql = q.toLowerCase();
|
|
||||||
const hits: SearchHit[] = [];
|
const hits: SearchHit[] = [];
|
||||||
|
const regex = new RegExp(escapeRegExp(q), "gi");
|
||||||
|
|
||||||
for (const doc of docs) {
|
for (const doc of docs) {
|
||||||
const lines = doc.text.split("\n");
|
const text = doc.text;
|
||||||
for (let i = 0; i < lines.length; i++) {
|
regex.lastIndex = 0;
|
||||||
if (lines[i].toLowerCase().includes(ql)) {
|
|
||||||
|
let match;
|
||||||
|
let lastNewlineIdx = -1;
|
||||||
|
let lineIdx = 0;
|
||||||
|
|
||||||
|
while ((match = regex.exec(text)) !== null) {
|
||||||
|
let nextNewline = text.indexOf("\n", lastNewlineIdx + 1);
|
||||||
|
while (nextNewline !== -1 && nextNewline < match.index) {
|
||||||
|
lineIdx++;
|
||||||
|
lastNewlineIdx = nextNewline;
|
||||||
|
nextNewline = text.indexOf("\n", lastNewlineIdx + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
let lineEnd = text.indexOf("\n", match.index);
|
||||||
|
if (lineEnd === -1) lineEnd = text.length;
|
||||||
|
|
||||||
|
const lineStart = lastNewlineIdx + 1;
|
||||||
|
const matchedLine = text.slice(lineStart, lineEnd);
|
||||||
|
|
||||||
hits.push({
|
hits.push({
|
||||||
docId: doc.id,
|
docId: doc.id,
|
||||||
docTitle: doc.title,
|
docTitle: doc.title,
|
||||||
line: i + 1,
|
line: lineIdx + 1,
|
||||||
text: lines[i],
|
text: matchedLine,
|
||||||
});
|
});
|
||||||
if (hits.length >= 200) return hits;
|
if (hits.length >= 200) return hits;
|
||||||
}
|
|
||||||
|
regex.lastIndex = lineEnd;
|
||||||
|
lastNewlineIdx = lineEnd;
|
||||||
|
lineIdx++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return hits;
|
return hits;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue