From 577d7a3516fcfead4873b64cf4834e25c66556e9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 10 Sep 2026 08:49:21 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20search=20by=20caching=20?= =?UTF-8?q?lowercased=20lines?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> --- src/ui/search.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/ui/search.ts b/src/ui/search.ts index 1009ff1..00485d3 100644 --- a/src/ui/search.ts +++ b/src/ui/search.ts @@ -33,6 +33,18 @@ export interface SearchController { const $ = (sel: string): T => document.querySelector(sel) as T; +const lineCache = new Map(); + +function getCachedDocLines(id: string, text: string): { lines: string[]; lowerLines: string[] } { + let cached = lineCache.get(id); + if (cached && cached.text === text) return cached; + const lines = text.split("\n"); + const lowerLines = text.toLowerCase().split("\n"); + cached = { text, lines, lowerLines }; + lineCache.set(id, cached); + return cached; +} + export function parseResultQuery( engine: SumEngine, q: string, @@ -81,7 +93,7 @@ export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] { break; } if (match) { - if (!lines) lines = doc.text.split("\n"); + if (!lines) lines = getCachedDocLines(doc.id, doc.text).lines; hits.push({ docId: doc.id, docTitle: doc.title, @@ -98,9 +110,9 @@ export function searchAllSheets(deps: SearchDeps, query: string): SearchHit[] { const ql = q.toLowerCase(); const hits: SearchHit[] = []; for (const doc of docs) { - const lines = doc.text.split("\n"); - for (let i = 0; i < lines.length; i++) { - if (lines[i].toLowerCase().includes(ql)) { + const { lines, lowerLines } = getCachedDocLines(doc.id, doc.text); + for (let i = 0; i < lowerLines.length; i++) { + if (lowerLines[i].includes(ql)) { hits.push({ docId: doc.id, docTitle: doc.title,