From 647352f6a0f3bf46cb2989e708c42eb1570fdacf Mon Sep 17 00:00:00 2001 From: yoominho Date: Thu, 30 Jul 2026 12:04:08 +0900 Subject: [PATCH] perf: O(1) title lookup in markdown import merge (#13117) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit collectDocumentsAndAttachments merged a folder/file sharing a title by scanning the sibling list with out.find(d => d.title === child.title) once per child — O(n²) over the number of siblings in one directory, which for a flat import can be large. Index the siblings by title in a Map and look up in O(1). First occurrence wins, matching find; the merge still mutates the same object. --- server/queues/tasks/MarkdownAPIImportTask.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/server/queues/tasks/MarkdownAPIImportTask.ts b/server/queues/tasks/MarkdownAPIImportTask.ts index 8e41792bd6..83815051f2 100644 --- a/server/queues/tasks/MarkdownAPIImportTask.ts +++ b/server/queues/tasks/MarkdownAPIImportTask.ts @@ -665,6 +665,16 @@ export default class MarkdownAPIImportTask extends APIImportTask { manifest: MarkdownAttachmentManifestItem[]; markdownByNode: Map; }): void { + // Index existing docs by title so the folder/file merge lookup below is O(1) + // instead of an `out.find` scan per child (O(n²) over the number of siblings + // in a single directory). First occurrence wins, mirroring `find`. + const byTitle = new Map(); + for (const doc of out) { + if (!byTitle.has(doc.title)) { + byTitle.set(doc.title, doc); + } + } + for (const child of children) { if (child.children.length > 0 && this.isAttachmentFolder(child)) { this.collectAttachments(child, manifest); @@ -691,7 +701,7 @@ export default class MarkdownAPIImportTask extends APIImportTask { // Folder-and-file with the same title (a "name.md" alongside a "name/" // directory) is merged onto a single document: the folder body picks up // the file's markdown text, and the folder's contents become children. - const sibling = out.find((d) => d.title === child.title); + const sibling = byTitle.get(child.title); if (sibling) { if (sibling.markdownText === "" && markdownText) { @@ -720,6 +730,7 @@ export default class MarkdownAPIImportTask extends APIImportTask { children: [], }; out.push(node); + byTitle.set(node.title, node); if (isFolder) { this.collectDocumentsAndAttachments({