From 5ca517aa0f71d57c8f69d521f708184b97398a0e Mon Sep 17 00:00:00 2001 From: ly-wang19 Date: Tue, 9 Jun 2026 11:26:23 +0800 Subject: [PATCH] perf(generation): index assigned images by id in fixElementDefaults MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixElementDefaults looked up each image element's metadata with assignedImages.find inside the per-element map, making the pass O(elements × images). Build a Map once and use an O(1) Map.get lookup instead. Behavior is identical (Map.get returns the same entry as the find), so it's purely a lookup-mechanism swap. Closes #700 --- lib/generation/scene-generator.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/generation/scene-generator.ts b/lib/generation/scene-generator.ts index 7548ef242e..ffab631df1 100644 --- a/lib/generation/scene-generator.ts +++ b/lib/generation/scene-generator.ts @@ -502,6 +502,11 @@ function fixElementDefaults( elements: GeneratedSlideData['elements'], assignedImages?: PdfImage[], ): GeneratedSlideData['elements'] { + // Index assigned images by id once (O(m)) so the per-image-element lookup + // below is O(1) instead of a `.find` nested inside this map (which made the + // pass O(elements × images)). + const imageMetaById = new Map((assignedImages ?? []).map((img) => [img.id, img])); + return elements.map((el) => { // Fix line elements if (el.type === 'line') { @@ -561,7 +566,7 @@ function fixElementDefaults( // Correct dimensions using known aspect ratio (src is still img_id at this point) if (assignedImages && typeof imageEl.src === 'string') { - const imgMeta = assignedImages.find((img) => img.id === imageEl.src); + const imgMeta = imageMetaById.get(imageEl.src); if (imgMeta?.width && imgMeta?.height) { const knownRatio = imgMeta.width / imgMeta.height; const curW = (el.width || 400) as number;