Skip to content

Build your personal knowledge base with Trilium Notes

perf(autocomplete): debounce from the keystroke, not the note count

Elian Doranf46c1ceb3df6
diff --git a/apps/client/src/services/note_autocomplete.spec.ts b/apps/client/src/services/note_autocomplete.spec.ts
index 948e08eb6c..2e7dfd0cd5 100644
--- a/apps/client/src/services/note_autocomplete.spec.ts
+++ b/apps/client/src/services/note_autocomplete.spec.ts
@@ -226,43 +226,6 @@ describe("autocompleteSource (via dataset)", () => {
expect(rows[0].commandId).toBe("c");
});
- it("resets searchDelay back to the computed value after it was zeroed", async () => {
- server.get = vi.fn(async () => []) as typeof server.get;
- // Capture the delay arg the source's debounce schedules with, so we can
- // observe that the zeroed searchDelay is restored to the computed value.
- const scheduledDelays: number[] = [];
- const originalSetTimeout = globalThis.setTimeout;
- const setTimeoutSpy = vi.spyOn(globalThis, "setTimeout").mockImplementation(((fn: any, delay?: number, ...rest: any[]) => {
- scheduledDelays.push(delay as number);
- // call through to the real timer so runSource's cb still resolves
- return originalSetTimeout(fn, delay as number, ...rest);
- }) as unknown as typeof setTimeout);
- try {
- const { $el, dataset } = initAndGetSource();
- // showRecentNotes zeroes the shared module-level searchDelay...
- noteAutocomplete.showRecentNotes($el);
- // ...so the FIRST source invocation debounces with delay 0 (immediate)
- // and, because searchDelay === 0, restores it to getSearchDelay(notesCount).
- await runSource(dataset, "x");
- // ...and the SECOND source invocation now sees the restored (non-zero)
- // delay rather than 0 — this is the reset behaviour the test name claims.
- await runSource(dataset, "y");
- expect(server.get).toHaveBeenCalled();
-
- // The source-callback delays we captured (filtering out any unrelated
- // timers froca/setup may schedule): the run after zeroing uses 0, the
- // following run uses the restored computed delay (which is NOT 0).
- // notesCount resolves to undefined in this mocked environment, so
- // getSearchDelay(undefined) === NaN — still distinct from the zeroed 0.
- expect(scheduledDelays).toContain(0); // the immediate run after zeroing
- const restored = scheduledDelays[scheduledDelays.length - 1];
- expect(restored).not.toBe(0); // searchDelay was restored away from 0
- expect(Number.isNaN(restored)).toBe(true); // == getSearchDelay(undefined)
- } finally {
- setTimeoutSpy.mockRestore();
- }
- });
-
it("queries the server and returns plain results", async () => {
server.get = vi.fn(async () => [{ noteTitle: "Result", notePath: "root/x" }]) as typeof server.get;
const { dataset } = initAndGetSource();
@@ -467,7 +430,7 @@ describe("autocompleteSource (via dataset)", () => {
});
});
-describe("source debounce + searchDelay reset", () => {
+describe("source debounce", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.useFakeTimers();
@@ -481,6 +444,57 @@ describe("source debounce + searchDelay reset", () => {
vi.useRealTimers();
});
+ it("runs the first search of a burst without waiting", () => {
+ const { dataset } = initAndGetSource();
+ dataset.source("hi", vi.fn());
+ // No timer is advanced: an idle input queries on the keystroke itself.
+ expect(server.get).toHaveBeenCalledTimes(1);
+ });
+
+ it("coalesces the rest of a burst into one search for the final term", async () => {
+ const { dataset } = initAndGetSource();
+ for (const term of ["h", "he", "hel", "hell"]) {
+ dataset.source(term, vi.fn());
+ }
+ expect(server.get).toHaveBeenCalledTimes(1);
+
+ await vi.runAllTimersAsync();
+ expect(server.get).toHaveBeenCalledTimes(2);
+ expect(server.get).toHaveBeenLastCalledWith(expect.stringContaining("query=hell"));
+ });
+
+ it("holds the window open while typing continues instead of pacing searches", async () => {
+ const { dataset } = initAndGetSource();
+ // Keystrokes arriving closer together than the window: only the one that opens the burst
+ // and the one after it ends may reach the server.
+ dataset.source("h", vi.fn());
+ for (const term of ["he", "hel", "hell"]) {
+ await vi.advanceTimersByTimeAsync(30);
+ dataset.source(term, vi.fn());
+ }
+ expect(server.get).toHaveBeenCalledTimes(1);
+
+ await vi.runAllTimersAsync();
+ expect(server.get).toHaveBeenCalledTimes(2);
+ expect(server.get).toHaveBeenLastCalledWith(expect.stringContaining("query=hell"));
+ });
+
+ it("gives each input its own timer, so one cannot cancel another's pending search", async () => {
+ const first = initAndGetSource();
+ const second = initAndGetSource();
+ // The leading-edge search of each burst, so both inputs hold a debounced one afterwards.
+ first.dataset.source("a1", vi.fn());
+ second.dataset.source("b1", vi.fn());
+ // Typing in one input must not drop the search the other is waiting on.
+ first.dataset.source("a2", vi.fn());
+ second.dataset.source("b2", vi.fn());
+ await vi.runAllTimersAsync();
+
+ const queries = vi.mocked(server.get).mock.calls.map(([url]) => url);
+ expect(queries.some((url) => url.includes("query=a2"))).toBe(true);
+ expect(queries.some((url) => url.includes("query=b2"))).toBe(true);
+ });
+
it("debounces and skips the search while composing input", async () => {
const { $el, dataset } = initAndGetSource();
// simulate IME composition active
diff --git a/apps/client/src/services/note_autocomplete.ts b/apps/client/src/services/note_autocomplete.ts
index 21cc919b13..f2f74cb007 100644
--- a/apps/client/src/services/note_autocomplete.ts
+++ b/apps/client/src/services/note_autocomplete.ts
@@ -16,17 +16,36 @@ const SELECTED_NOTE_PATH_KEY = "data-note-path";
const SELECTED_EXTERNAL_LINK_KEY = "data-external-link";
-// To prevent search lag when there are a large number of notes, set a delay based on the number of notes to avoid jitter.
-const notesCount = await server.get<number>(`autocomplete/notesCount`);
-let debounceTimeoutId: ReturnType<typeof setTimeout>;
-
-function getSearchDelay(notesCount: number): number {
- const maxNotes = 20000;
- const maxDelay = 1000;
- const delay = Math.min(maxDelay, (notesCount / maxNotes) * maxDelay);
- return delay;
+// Short on purpose: a search costs tens of milliseconds, so this exists to merge keystrokes that
+// arrive faster than a person reads a result, not to wait out a slow server. Anything longer is
+// felt on every keystroke typed at a normal pace.
+const SEARCH_DEBOUNCE_MS = 50;
+
+/**
+ * Debounces one input's searches: the first keystroke after a pause queries immediately, and a burst
+ * typed faster than {@link SEARCH_DEBOUNCE_MS} collapses into one search that runs once it stops.
+ * Each input holds its own timer, so a keystroke in one cannot cancel a search another is waiting on.
+ */
+function createSearchScheduler() {
+ let timeoutId: ReturnType<typeof setTimeout> | undefined;
+ let lastCallAt = 0;
+
+ return (runSearch: () => void) => {
+ clearTimeout(timeoutId);
+
+ // Measured from the previous keystroke rather than the previous search. Measuring from the
+ // search paces requests at a fixed rate instead of ending the window when typing pauses.
+ const now = Date.now();
+ const startsBurst = now - lastCallAt >= SEARCH_DEBOUNCE_MS;
+ lastCallAt = now;
+
+ if (startsBurst) {
+ runSearch();
+ } else {
+ timeoutId = setTimeout(runSearch, SEARCH_DEBOUNCE_MS);
+ }
+ };
}
-let searchDelay = getSearchDelay(notesCount);
async function getInboxTarget() {
try {
@@ -217,7 +236,6 @@ async function autocompleteSource(term: string, cb: (rows: Suggestion[]) => void
// fires one to keep consumers bound to it, such as NoteAutocomplete's onTextChange, in sync.
function clearText($el: JQuery<HTMLElement>) {
- searchDelay = 0;
$el.setSelectedNotePath("");
$el.autocomplete("val", "").trigger("change");
$el.trigger("input");
@@ -231,7 +249,6 @@ function setText($el: JQuery<HTMLElement>, text: string) {
}
function showRecentNotes($el: JQuery<HTMLElement>) {
- searchDelay = 0;
$el.setSelectedNotePath("");
$el.autocomplete("val", "");
$el.trigger("input");
@@ -240,7 +257,6 @@ function showRecentNotes($el: JQuery<HTMLElement>) {
}
function showAllCommands($el: JQuery<HTMLElement>) {
- searchDelay = 0;
$el.setSelectedNotePath("");
$el.autocomplete("val", ">");
$el.trigger("input");
@@ -256,7 +272,6 @@ function fullTextSearch($el: JQuery<HTMLElement>, options: Options) {
options.fastSearch = false;
$el.autocomplete("val", "");
$el.setSelectedNotePath("");
- searchDelay = 0;
$el.autocomplete("val", searchString);
}
@@ -270,6 +285,8 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
options = options || {};
+ const scheduleSearch = createSearchScheduler();
+
// Used to track whether the user is performing character composition with an input method (such as Chinese Pinyin, Japanese, Korean, etc.) and to avoid triggering a search during the composition process.
let isComposingInput = false;
$el.on("compositionstart", () => {
@@ -369,17 +386,12 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
[
{
source: (term, cb) => {
- clearTimeout(debounceTimeoutId);
- debounceTimeoutId = setTimeout(() => {
+ scheduleSearch(() => {
if (isComposingInput) {
return;
}
autocompleteSource(term, cb, options);
- }, searchDelay);
-
- if (searchDelay === 0) {
- searchDelay = getSearchDelay(notesCount);
- }
+ });
},
displayKey: "notePathTitle",
templates: {
diff --git a/packages/trilium-core/src/routes/api/autocomplete.spec.ts b/packages/trilium-core/src/routes/api/autocomplete.spec.ts
index cc9b9e82dd..8f748788ba 100644
--- a/packages/trilium-core/src/routes/api/autocomplete.spec.ts
+++ b/packages/trilium-core/src/routes/api/autocomplete.spec.ts
@@ -91,19 +91,4 @@ describe("Autocomplete API (core)", () => {
expect(res.status).toBe(400);
});
});
-
- describe("getNotesCount", () => {
- it("returns the count of non-deleted notes as a number", async () => {
- const before = await api.get<number>("/api/autocomplete/notesCount");
- expect(before.status).toBe(200);
- expect(typeof before.body).toBe("number");
- expect(before.body).toBeGreaterThan(0);
-
- await createTextNote(api, { title: "Counts towards total" });
-
- const after = await api.get<number>("/api/autocomplete/notesCount");
- expect(after.status).toBe(200);
- expect(after.body).toBeGreaterThan(before.body);
- });
- });
});
diff --git a/packages/trilium-core/src/routes/api/autocomplete.ts b/packages/trilium-core/src/routes/api/autocomplete.ts
index 26edd86b46..bcc7eb4bb0 100644
--- a/packages/trilium-core/src/routes/api/autocomplete.ts
+++ b/packages/trilium-core/src/routes/api/autocomplete.ts
@@ -4,7 +4,6 @@ import becca from "../../becca/becca.js";
import * as cls from "../../services/context.js";
import { getLog } from "../../services/log.js";
import searchService from "../../services/search/services/search.js";
-import { getSql } from "../../services/sql/index.js";
import { escapeHtml } from "../../services/utils/index.js";
import { ValidationError } from "../../errors.js";
import becca_service from "../../becca/becca_service.js";
@@ -80,15 +79,6 @@ function getRecentNotes(activeNoteId: string) {
});
}
-// Get the total number of notes
-function getNotesCount(req: Request) {
- const notesCount = getSql().getRow(
- /*sql*/`SELECT COUNT(*) AS count FROM notes WHERE isDeleted = 0;`,
- ) as { count: number };
- return notesCount.count;
-}
-
export default {
- getAutocomplete,
- getNotesCount
+ getAutocomplete
};
diff --git a/packages/trilium-core/src/routes/index.ts b/packages/trilium-core/src/routes/index.ts
index 2a1df91547..11d2d5d87c 100644
--- a/packages/trilium-core/src/routes/index.ts
+++ b/packages/trilium-core/src/routes/index.ts
@@ -250,7 +250,6 @@ export function buildSharedApiRoutes({ route, asyncRoute, asyncRouteWithoutTrans
asyncApiRoute(PST, "/api/llm-chat/provider-models", llmRoute.getProviderModels);
apiRoute(GET, "/api/autocomplete", autocompleteApiRoute.getAutocomplete);
- apiRoute(GET, "/api/autocomplete/notesCount", autocompleteApiRoute.getNotesCount);
apiRoute(PUT, "/api/notes/:noteId/clone-to-branch/:parentBranchId", cloningApiRoute.cloneNoteToBranch);
apiRoute(PUT, "/api/notes/:noteId/toggle-in-parent/:parentNoteId/:present", cloningApiRoute.toggleNoteInParent);