Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

perf(unified-search): skip redundant loading-spinner DOM rebuild on every keystroke (#6621)

LearningCircuit783967df94fd
diff --git a/changelog.d/5579.misc.md b/changelog.d/5579.misc.md
new file mode 100644
index 0000000000..8e071d439c
--- /dev/null
+++ b/changelog.d/5579.misc.md
@@ -0,0 +1 @@
+The "Search everything" page no longer tears down and rebuilds the loading spinner's DOM on every keystroke — consecutive searches while the spinner is already visible reuse the existing nodes (updating only the label if the message changes), reducing per-keystroke DOM churn during fast typing on slow devices.
diff --git a/src/local_deep_research/web/static/js/pages/unified_search.js b/src/local_deep_research/web/static/js/pages/unified_search.js
index c79d7cd256..670144ad7a 100644
--- a/src/local_deep_research/web/static/js/pages/unified_search.js
+++ b/src/local_deep_research/web/static/js/pages/unified_search.js
@@ -62,6 +62,10 @@ let unifiedSearchMode = UNIFIED_SEARCH_DEFAULT_MODE;
let unifiedSearchAbortController = null;
// Monotonic id so a slow response can't overwrite a newer search.
let unifiedSearchRunId = 0;
+// Coarse results-area state ('idle' | 'loading' | 'results' | 'error') so
+// the per-keystroke loading call can short-circuit when the spinner is
+// already on screen instead of rebuilding its DOM every input event.
+let unifiedSearchUiState = 'idle';
// Debounce timer for the search input. Module-level so a mode change can
// cancel a pending re-run — it re-runs immediately itself, and the stale
// timer would otherwise fire a duplicate identical search.
@@ -225,6 +229,21 @@ function clearUnifiedSearchNotice() {
/** Render the centered "searching…" spinner inside the results area. */
function showUnifiedSearchLoading(message) {
if (!unifiedSearchResults) return;
+ if (unifiedSearchUiState === 'loading') {
+ // The spinner from the previous keystroke is still correct — a
+ // rebuild here is pure DOM churn on the hottest path (every
+ // input event above MIN_QUERY_LENGTH). Update only the label
+ // when the message actually differs.
+ const label = unifiedSearchResults.querySelector(
+ '.ldr-semantic-search-loading > p'
+ );
+ const text = message || 'Searching...';
+ if (label && label.textContent !== text) {
+ label.textContent = text;
+ }
+ return;
+ }
+ unifiedSearchUiState = 'loading';
unifiedSearchResults.replaceChildren();
const wrap = document.createElement('div');
wrap.className = 'ldr-semantic-search-loading';
@@ -245,6 +264,7 @@ function showUnifiedSearchLoading(message) {
*/
function showUnifiedSearchError(message) {
if (!unifiedSearchResults || !unifiedSearchEmpty) return;
+ unifiedSearchUiState = 'error';
unifiedSearchResults.replaceChildren();
unifiedSearchResults.style.display = 'none';
const icon = document.createElement('i');
@@ -268,6 +288,7 @@ function showUnifiedSearchError(message) {
/** Render the idle state shown before the user has typed a query. */
function showUnifiedSearchIdleState() {
if (!unifiedSearchResults || !unifiedSearchEmpty) return;
+ unifiedSearchUiState = 'idle';
unifiedSearchResults.replaceChildren();
unifiedSearchResults.style.display = 'none';
unifiedSearchEmpty.innerHTML = `
@@ -520,6 +541,7 @@ function createUnifiedSearchResultCard(result) {
/** Render the result list (or the "No matches" empty state). */
function renderUnifiedSearchResults(results) {
if (!unifiedSearchResults || !unifiedSearchEmpty) return;
+ unifiedSearchUiState = 'results';
if (!results.length) {
unifiedSearchResults.replaceChildren();
@@ -572,6 +594,7 @@ if (typeof window !== 'undefined' && window.__VITEST_TEST__) {
unifiedSearchModeMenu = modeMenu;
unifiedSearchModeLabel = modeLabel;
unifiedSearchRunId = 0;
+ unifiedSearchUiState = 'idle';
},
};
}
diff --git a/tests/js/pages/unified-search.test.js b/tests/js/pages/unified-search.test.js
index 8735bcdd33..14cd22074c 100644
--- a/tests/js/pages/unified-search.test.js
+++ b/tests/js/pages/unified-search.test.js
@@ -104,6 +104,59 @@ describe('mode registry drives the selector UI', () => {
});
describe('input invalidation', () => {
+ it('skips the redundant spinner rebuild on consecutive keystrokes', async () => {
+ // Typing fires the input handler per keystroke and each one used
+ // to rebuild the spinner DOM (replaceChildren + createElement) even
+ // though the previous keystroke's spinner was still on screen —
+ // pure per-keystroke churn. Node IDENTITY is the assertion:
+ // childElementCount stays 1 across a replacement too.
+ vi.useFakeTimers();
+
+ try {
+ const dom = makeDom();
+ hook.setDomRefs(dom);
+ hook.setUnifiedSearchMode('text');
+ hook.setupUnifiedSearchListeners();
+
+ globalThis.safeFetch = vi.fn(() =>
+ Promise.resolve({
+ json: () => Promise.resolve({
+ success: true,
+ results: [{
+ id: 'needle',
+ title: 'Needle result',
+ content_preview: 'haystack',
+ url: '/notes/needle',
+ source_type: 'note',
+ }],
+ }),
+ })
+ );
+
+ dom.input.value = 'needle';
+ dom.input.dispatchEvent(new Event('input'));
+ expect(dom.results.childElementCount).toBe(1);
+ const spinnerAfterFirst = dom.results.firstElementChild;
+ expect(spinnerAfterFirst.className).toContain(
+ 'ldr-semantic-search-loading'
+ );
+
+ dom.input.value = 'needles';
+ dom.input.dispatchEvent(new Event('input'));
+ // Same node instance — no teardown/rebuild between keystrokes.
+ expect(dom.results.firstElementChild).toBe(spinnerAfterFirst);
+ expect(dom.results.childElementCount).toBe(1);
+
+ // ...and the debounce still lands: results replace the spinner.
+ await vi.advanceTimersByTimeAsync(300);
+ expect(dom.results.textContent).toContain('Needle result');
+ expect(dom.results.firstElementChild).not.toBe(spinnerAfterFirst);
+ } finally {
+ vi.clearAllTimers();
+ vi.useRealTimers();
+ }
+ });
+
it('aborts an in-flight search before the replacement debounce fires', async () => {
vi.useFakeTimers();
let resolveOld;