From 5b6640eab34375fe64d15673b52c90426d9d4478 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 3 Apr 2026 13:20:06 +0200 Subject: [PATCH] A couple of small `collectAnnotationsByType` improvements - Use the same `PartialEvaluator` instance for all annotations on the page, to reduce unnecessary object creation. - Use `Object.hasOwn` to check if the annotations were already parsed, to avoid having to keep a separate boolean variable in-sync with the actual code. --- src/core/document.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index f91a9f29b..66d142d8c 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -83,8 +83,6 @@ import { XRef } from "./xref.js"; const LETTER_SIZE_MEDIABOX = [0, 0, 612, 792]; class Page { - #areAnnotationsCached = false; - #resourcesPromise = null; constructor({ @@ -874,8 +872,6 @@ class Page { return sortedAnnotations; }); - this.#areAnnotationsCached = true; - return shadow(this, "_parsedAnnotations", promise); } @@ -897,7 +893,7 @@ class Page { ) { const { pageIndex } = this; - if (this.#areAnnotationsCached) { + if (Object.hasOwn(this, "_parsedAnnotations")) { const cachedAnnotations = await this._parsedAnnotations; for (const { data } of cachedAnnotations) { if (!types || types.has(data.annotationType)) { @@ -909,6 +905,8 @@ class Page { } const annots = await this.pdfManager.ensure(this, "annotations"); + let partialEvaluator; + for (const annotationRef of annots) { promises.push( AnnotationFactory.create( @@ -927,7 +925,8 @@ class Page { } annotation.data.pageIndex = pageIndex; if (annotation.hasTextContent && annotation.viewable) { - const partialEvaluator = this.#createPartialEvaluator(handler); + partialEvaluator ??= this.#createPartialEvaluator(handler); + await annotation.extractTextContent(partialEvaluator, task, [ -Infinity, -Infinity,