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.
This commit is contained in:
Jonas Jenwald 2026-04-03 13:20:06 +02:00
parent 2414f54675
commit 5b6640eab3

View File

@ -83,8 +83,6 @@ import { XRef } from "./xref.js";
const LETTER_SIZE_MEDIABOX = [0, 0, 612, 792]; const LETTER_SIZE_MEDIABOX = [0, 0, 612, 792];
class Page { class Page {
#areAnnotationsCached = false;
#resourcesPromise = null; #resourcesPromise = null;
constructor({ constructor({
@ -874,8 +872,6 @@ class Page {
return sortedAnnotations; return sortedAnnotations;
}); });
this.#areAnnotationsCached = true;
return shadow(this, "_parsedAnnotations", promise); return shadow(this, "_parsedAnnotations", promise);
} }
@ -897,7 +893,7 @@ class Page {
) { ) {
const { pageIndex } = this; const { pageIndex } = this;
if (this.#areAnnotationsCached) { if (Object.hasOwn(this, "_parsedAnnotations")) {
const cachedAnnotations = await this._parsedAnnotations; const cachedAnnotations = await this._parsedAnnotations;
for (const { data } of cachedAnnotations) { for (const { data } of cachedAnnotations) {
if (!types || types.has(data.annotationType)) { if (!types || types.has(data.annotationType)) {
@ -909,6 +905,8 @@ class Page {
} }
const annots = await this.pdfManager.ensure(this, "annotations"); const annots = await this.pdfManager.ensure(this, "annotations");
let partialEvaluator;
for (const annotationRef of annots) { for (const annotationRef of annots) {
promises.push( promises.push(
AnnotationFactory.create( AnnotationFactory.create(
@ -927,7 +925,8 @@ class Page {
} }
annotation.data.pageIndex = pageIndex; annotation.data.pageIndex = pageIndex;
if (annotation.hasTextContent && annotation.viewable) { if (annotation.hasTextContent && annotation.viewable) {
const partialEvaluator = this.#createPartialEvaluator(handler); partialEvaluator ??= this.#createPartialEvaluator(handler);
await annotation.extractTextContent(partialEvaluator, task, [ await annotation.extractTextContent(partialEvaluator, task, [
-Infinity, -Infinity,
-Infinity, -Infinity,