Skip to content

Commit 58996f2

Browse files
committed
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.
1 parent 40bd735 commit 58996f2

3 files changed

Lines changed: 6 additions & 6 deletions

File tree

src/core/annotation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3398,7 +3398,7 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
33983398
? this.data.fieldValue
33993399
: "Yes";
34003400

3401-
const exportValues = this._decodeFormValue(normalAppearance.getKeys());
3401+
const exportValues = this._decodeFormValue([...normalAppearance.getKeys()]);
34023402
if (exportValues.length === 0) {
34033403
exportValues.push("Off", yes);
34043404
} else if (exportValues.length === 1) {

src/core/primitives.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class Dict {
180180
}
181181

182182
getKeys() {
183-
return [...this._map.keys()];
183+
return this._map.keys();
184184
}
185185

186186
// No dereferencing.

test/unit/primitives_spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ describe("primitives", function () {
276276

277277
it("should get all key names", function () {
278278
const expectedKeys = ["FontFile", "FontFile2", "FontFile3"];
279-
const keys = dictWithManyKeys.getKeys();
279+
const keys = [...dictWithManyKeys.getKeys()];
280280

281281
expect(keys.sort()).toEqual(expectedKeys);
282282
});
@@ -337,7 +337,7 @@ describe("primitives", function () {
337337
xref: null,
338338
dictArray: [dictWithManyKeys, dictWithSizeKey, fontFileDict],
339339
});
340-
const mergedKeys = mergedDict.getKeys();
340+
const mergedKeys = [...mergedDict.getKeys()];
341341

342342
expect(mergedKeys.sort()).toEqual(expectedKeys);
343343
expect(mergedDict.get("FontFile")).toEqual(testFontFile);
@@ -374,8 +374,8 @@ describe("primitives", function () {
374374
expect(mergedFontDict instanceof Dict).toEqual(true);
375375
expect(mergedSubFontDict instanceof Dict).toEqual(true);
376376

377-
const mergedFontDictKeys = mergedFontDict.getKeys();
378-
const mergedSubFontDictKeys = mergedSubFontDict.getKeys();
377+
const mergedFontDictKeys = [...mergedFontDict.getKeys()];
378+
const mergedSubFontDictKeys = [...mergedSubFontDict.getKeys()];
379379

380380
expect(mergedFontDictKeys).toEqual(["F1"]);
381381
expect(mergedSubFontDictKeys).toEqual(["F1", "F2", "F3"]);

0 commit comments

Comments
 (0)