From 8bbb7c88d3936df33fda7fa5536f5dd4a21f6cf3 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 9 Mar 2026 16:11:21 +0100 Subject: [PATCH 1/2] Change the `AnnotationLayer.prototype.getEditableAnnotations` method to return an iterator This method is only used with loops, and it should be a tiny bit more efficient to use an iterator directly rather than first iterating through the underlying `Map` to create a temporary `Array` that we finally iterate through at the call-site. --- src/display/annotation_layer.js | 2 +- src/display/editor/annotation_editor_layer.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js index 7a992484605d4..d131b145a7e11 100644 --- a/src/display/annotation_layer.js +++ b/src/display/annotation_layer.js @@ -4100,7 +4100,7 @@ class AnnotationLayer { } getEditableAnnotations() { - return Array.from(this.#editableAnnotations.values()); + return this.#editableAnnotations.values(); } getEditableAnnotation(id) { diff --git a/src/display/editor/annotation_editor_layer.js b/src/display/editor/annotation_editor_layer.js index 16a118735210f..11c982fbf6007 100644 --- a/src/display/editor/annotation_editor_layer.js +++ b/src/display/editor/annotation_editor_layer.js @@ -416,8 +416,7 @@ class AnnotationEditorLayer { } // Show the annotations that were hidden in enable(). - const editables = annotationLayer.getEditableAnnotations(); - for (const editable of editables) { + for (const editable of annotationLayer.getEditableAnnotations()) { const { id } = editable.data; if (this.#uiManager.isDeletedAnnotationElement(id)) { editable.updateEdited({ deleted: true }); From dbb6ffb8d5b390ec816fa9b6d262df6db1ff18ab Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 9 Mar 2026 16:18:48 +0100 Subject: [PATCH 2/2] Change the `Font.prototype.glyphCacheValues` method to return an iterator This method is only used with loops, and it should be a tiny bit more efficient to use an iterator directly rather than first iterating through the underlying data to create a temporary `Array` that we finally iterate through at the call-site. *Please note:* As port of these changes the chars/glyph caches, on the `Font` instances, are changed to use `Map`s rather than Objects. --- src/core/fonts.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/core/fonts.js b/src/core/fonts.js index 499cc263f5a7b..901a350f55253 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -969,6 +969,10 @@ function createNameTable(name, proto) { * decoding logics whatever type it is (assuming the font type is supported). */ class Font { + #charsCache = new Map(); + + #glyphCache = new Map(); + constructor(name, file, properties, evaluatorOptions) { this.name = name; this.psName = null; @@ -981,9 +985,6 @@ class Font { this.missingFile = false; this.cssFontInfo = properties.cssFontInfo; - this._charsCache = Object.create(null); - this._glyphCache = Object.create(null); - let isSerifFont = !!(properties.flags & FontFlags.Serif); // Fallback to checking the font name, in order to improve text-selection, // since the /Flags-entry is often wrong (fixes issue13845.pdf). @@ -3385,7 +3386,7 @@ class Font { * @private */ _charToGlyph(charcode, isSpace = false) { - let glyph = this._glyphCache[charcode]; + let glyph = this.#glyphCache.get(charcode); // All `Glyph`-properties, except `isSpace` in multi-byte strings, // depend indirectly on the `charcode`. if (glyph?.isSpace === isSpace) { @@ -3480,12 +3481,13 @@ class Font { isSpace, isInFont ); - return (this._glyphCache[charcode] = glyph); + this.#glyphCache.set(charcode, glyph); + return glyph; } charsToGlyphs(chars) { // If we translated this string before, just grab it from the cache. - let glyphs = this._charsCache[chars]; + let glyphs = this.#charsCache.get(chars); if (glyphs) { return glyphs; } @@ -3517,7 +3519,8 @@ class Font { } // Enter the translated string into the cache. - return (this._charsCache[chars] = glyphs); + this.#charsCache.set(chars, glyphs); + return glyphs; } /** @@ -3549,7 +3552,7 @@ class Font { } get glyphCacheValues() { - return Object.values(this._glyphCache); + return this.#glyphCache.values(); } /**