From 122d5e549a28d6b4909ca51525fc02c8260f376c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 22 Mar 2023 09:38:00 +0100 Subject: [PATCH] Track previous "XRefStm"s in a `Set`, rather than an `Object` Having just reviewed a patch touching this code, I couldn't help noticing that an `Object` isn't really the optimal data-structure for this and nowadays we can do better by using a `Set` instead. --- src/core/xref.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/core/xref.js b/src/core/xref.js index e32e1ab48..8b4d1549b 100644 --- a/src/core/xref.js +++ b/src/core/xref.js @@ -37,7 +37,7 @@ class XRef { this.stream = stream; this.pdfManager = pdfManager; this.entries = []; - this.xrefstms = Object.create(null); + this._xrefStms = new Set(); this._cacheMap = new Map(); // Prepare the XRef cache. this._pendingRefs = new RefSet(); this._newPersistentRefNum = null; @@ -540,7 +540,7 @@ class XRef { const xrefTagOffset = skipUntil(content, 0, xrefBytes); if (xrefTagOffset < contentLength && content[xrefTagOffset + 5] < 64) { xrefStms.push(position - stream.start); - this.xrefstms[position - stream.start] = 1; // Avoid recursion + this._xrefStms.add(position - stream.start); // Avoid recursion } position += contentLength; @@ -700,14 +700,11 @@ class XRef { // Recursively get other XRefs 'XRefStm', if any obj = dict.get("XRefStm"); - if (Number.isInteger(obj)) { - const pos = obj; + if (Number.isInteger(obj) && !this._xrefStms.has(obj)) { // ignore previously loaded xref streams // (possible infinite recursion) - if (!(pos in this.xrefstms)) { - this.xrefstms[pos] = 1; - this.startXRefQueue.push(pos); - } + this._xrefStms.add(obj); + this.startXRefQueue.push(obj); } } else if (Number.isInteger(obj)) { // Parse in-stream XRef @@ -757,8 +754,7 @@ class XRef { } get lastXRefStreamPos() { - const pos = Object.keys(this.xrefstms); - return pos.length === 0 ? null : Math.max(...pos); + return this._xrefStms.size > 0 ? Math.max(...this._xrefStms) : null; } getEntry(i) {