From 5a03b1c0d79f2b3f16a431f9474a6b6c8c2d8ad1 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sat, 6 Apr 2019 16:52:29 +0200 Subject: [PATCH] Optimize `convertOpList` in `svg.js` by computing the operator ID mapping only once There is no need to recompute this for every operator list we encounter. --- src/display/svg.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/display/svg.js b/src/display/svg.js index fe5e98550..f7935dadc 100644 --- a/src/display/svg.js +++ b/src/display/svg.js @@ -432,6 +432,14 @@ SVGGraphics = class SVGGraphics { this.embeddedFonts = Object.create(null); this.cssStyle = null; this.forceDataSchema = !!forceDataSchema; + + // In `src/shared/util.js` the operator names are mapped to IDs. + // The list below represents the reverse of that, i.e., it maps IDs + // to operator names. + this._operatorIdMapping = []; + for (const op in OPS) { + this._operatorIdMapping[OPS[op]] = op; + } } save() { @@ -493,11 +501,7 @@ SVGGraphics = class SVGGraphics { } convertOpList(operatorList) { - const REVOPS = []; - for (const op in OPS) { - REVOPS[OPS[op]] = op; - } - + const operatorIdMapping = this._operatorIdMapping; const argsArray = operatorList.argsArray; const fnArray = operatorList.fnArray; const opList = []; @@ -505,7 +509,7 @@ SVGGraphics = class SVGGraphics { const fnId = fnArray[i]; opList.push({ 'fnId': fnId, - 'fn': REVOPS[fnId], + 'fn': operatorIdMapping[fnId], 'args': argsArray[i], }); }