From 50d66d7d349494486e3fb98cfe1ae778133a534e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 4 Mar 2026 12:33:47 +0100 Subject: [PATCH] Use the `Dict.prototype.getRawEntries` method more This changes a number of loops currently using `Dict.prototype.{getKeys, getRaw}`, since it should be a tiny bit more efficient to use an iterator directly rather than first iterating through `Map`-keys to create a temporary `Array` that we finally iterate through at the call-site. Note that the `getKeys` method is much older than `getRawEntries`, and originally the `Dict` class stored its data in a regular `Object`, hence why the old code was written that way. --- src/core/annotation.js | 8 ++++---- src/core/core_utils.js | 5 ++--- src/core/primitives.js | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index a4d9546d3..3cea051ce 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -2228,9 +2228,9 @@ class WidgetAnnotation extends Annotation { } const dict = new Dict(xref); - for (const key of originalDict.getKeys()) { + for (const [key, rawVal] of originalDict.getRawEntries()) { if (key !== "AP") { - dict.set(key, originalDict.getRaw(key)); + dict.set(key, rawVal); } } if (flags !== undefined) { @@ -2442,8 +2442,8 @@ class WidgetAnnotation extends Annotation { if (this._fieldResources.mergedResources.has("Font")) { const oldFont = this._fieldResources.mergedResources.get("Font"); - for (const key of newFont.getKeys()) { - oldFont.set(key, newFont.getRaw(key)); + for (const [key, rawVal] of newFont.getRawEntries()) { + oldFont.set(key, rawVal); } } else { this._fieldResources.mergedResources.set("Font", newFont); diff --git a/src/core/core_utils.js b/src/core/core_utils.js index b1490c581..d0d801fb5 100644 --- a/src/core/core_utils.js +++ b/src/core/core_utils.js @@ -459,15 +459,14 @@ function collectActions(xref, dict, eventType) { if (!(additionalActions instanceof Dict)) { continue; } - for (const key of additionalActions.getKeys()) { + for (const [key, rawActionDict] of additionalActions.getRawEntries()) { const action = eventType[key]; if (!action) { continue; } - const actionDict = additionalActions.getRaw(key); const parents = new RefSet(); const list = []; - _collectJS(actionDict, xref, list, parents); + _collectJS(rawActionDict, xref, list, parents); if (list.length > 0) { actions[action] = list; } diff --git a/src/core/primitives.js b/src/core/primitives.js index 5afc807ec..121feadeb 100644 --- a/src/core/primitives.js +++ b/src/core/primitives.js @@ -312,8 +312,8 @@ class Dict { clone() { const dict = new Dict(this.xref); - for (const key of this.getKeys()) { - dict.set(key, this.getRaw(key)); + for (const [key, rawVal] of this.getRawEntries()) { + dict.set(key, rawVal); } return dict; }