From 58996f21b2995805522c93216cd9bf6f36e6ea09 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 4 Mar 2026 13:11:04 +0100 Subject: [PATCH 1/2] Change the `Dict.prototype.getKeys` method to return an iterator This method is usually used with loops, and 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 old code, 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 | 2 +- src/core/primitives.js | 2 +- test/unit/primitives_spec.js | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index 3cea051ce..d97c01a96 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -3398,7 +3398,7 @@ class ButtonWidgetAnnotation extends WidgetAnnotation { ? this.data.fieldValue : "Yes"; - const exportValues = this._decodeFormValue(normalAppearance.getKeys()); + const exportValues = this._decodeFormValue([...normalAppearance.getKeys()]); if (exportValues.length === 0) { exportValues.push("Off", yes); } else if (exportValues.length === 1) { diff --git a/src/core/primitives.js b/src/core/primitives.js index 121feadeb..5e4cdd460 100644 --- a/src/core/primitives.js +++ b/src/core/primitives.js @@ -180,7 +180,7 @@ class Dict { } getKeys() { - return [...this._map.keys()]; + return this._map.keys(); } // No dereferencing. diff --git a/test/unit/primitives_spec.js b/test/unit/primitives_spec.js index ed9993121..bf1988cb2 100644 --- a/test/unit/primitives_spec.js +++ b/test/unit/primitives_spec.js @@ -276,7 +276,7 @@ describe("primitives", function () { it("should get all key names", function () { const expectedKeys = ["FontFile", "FontFile2", "FontFile3"]; - const keys = dictWithManyKeys.getKeys(); + const keys = [...dictWithManyKeys.getKeys()]; expect(keys.sort()).toEqual(expectedKeys); }); @@ -337,7 +337,7 @@ describe("primitives", function () { xref: null, dictArray: [dictWithManyKeys, dictWithSizeKey, fontFileDict], }); - const mergedKeys = mergedDict.getKeys(); + const mergedKeys = [...mergedDict.getKeys()]; expect(mergedKeys.sort()).toEqual(expectedKeys); expect(mergedDict.get("FontFile")).toEqual(testFontFile); @@ -374,8 +374,8 @@ describe("primitives", function () { expect(mergedFontDict instanceof Dict).toEqual(true); expect(mergedSubFontDict instanceof Dict).toEqual(true); - const mergedFontDictKeys = mergedFontDict.getKeys(); - const mergedSubFontDictKeys = mergedSubFontDict.getKeys(); + const mergedFontDictKeys = [...mergedFontDict.getKeys()]; + const mergedSubFontDictKeys = [...mergedSubFontDict.getKeys()]; expect(mergedFontDictKeys).toEqual(["F1"]); expect(mergedSubFontDictKeys).toEqual(["F1", "F2", "F3"]); From 229e3642be8b84db5b70f4e29c2bb9549e54db60 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 4 Mar 2026 13:23:40 +0100 Subject: [PATCH 2/2] Change the `Dict.prototype.getRawValues` method to return an iterator This method is usually used with loops, and it should be a tiny bit more efficient to use an iterator directly rather than first iterating through ` Map`-values to create a temporary `Array` that we finally iterate through at the call-site. Note that the `getRawValues` method is old code, and originally the `Dict` class stored its data in a regular `Object`, hence why the old code was written that way. --- src/core/primitives.js | 2 +- test/unit/primitives_spec.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/primitives.js b/src/core/primitives.js index 5e4cdd460..e862bd538 100644 --- a/src/core/primitives.js +++ b/src/core/primitives.js @@ -185,7 +185,7 @@ class Dict { // No dereferencing. getRawValues() { - return [...this._map.values()]; + return this._map.values(); } getRawEntries() { diff --git a/test/unit/primitives_spec.js b/test/unit/primitives_spec.js index bf1988cb2..f8ec443d4 100644 --- a/test/unit/primitives_spec.js +++ b/test/unit/primitives_spec.js @@ -284,7 +284,7 @@ describe("primitives", function () { it("should get all raw values", function () { // Test direct objects: const expectedRawValues1 = [testFontFile, testFontFile2, testFontFile3]; - const rawValues1 = dictWithManyKeys.getRawValues(); + const rawValues1 = [...dictWithManyKeys.getRawValues()]; expect(rawValues1.sort()).toEqual(expectedRawValues1); @@ -305,7 +305,7 @@ describe("primitives", function () { dict.set("Contents", contentsRef); const expectedRawValues2 = [contentsRef, resourcesRef, typeName]; - const rawValues2 = dict.getRawValues(); + const rawValues2 = [...dict.getRawValues()]; expect(rawValues2.sort()).toEqual(expectedRawValues2); }); @@ -380,8 +380,8 @@ describe("primitives", function () { expect(mergedFontDictKeys).toEqual(["F1"]); expect(mergedSubFontDictKeys).toEqual(["F1", "F2", "F3"]); - const mergedFontDictValues = mergedFontDict.getRawValues(); - const mergedSubFontDictValues = mergedSubFontDict.getRawValues(); + const mergedFontDictValues = [...mergedFontDict.getRawValues()]; + const mergedSubFontDictValues = [...mergedSubFontDict.getRawValues()]; expect(mergedFontDictValues).toEqual(["Local font one"]); expect(mergedSubFontDictValues).toEqual([