From a24c94487c614f0f8f6f7a881bfa5e3b6ddc1c3b Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Sat, 25 Jan 2020 20:53:28 +0100 Subject: [PATCH] Cache label instructions for better performance --- src/ol/render/canvas/Executor.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/ol/render/canvas/Executor.js b/src/ol/render/canvas/Executor.js index e94f666554..c109c6d04b 100644 --- a/src/ol/render/canvas/Executor.js +++ b/src/ol/render/canvas/Executor.js @@ -173,6 +173,12 @@ class Executor { * @type {Object>} */ this.widths_ = {}; + + /** + * @private + * @type {Object} + */ + this.labels_ = {}; } /** @@ -183,6 +189,10 @@ class Executor { * @return {Label} Label. */ createLabel(text, textKey, fillKey, strokeKey) { + const key = text + textKey + fillKey + strokeKey; + if (this.labels_[key]) { + return this.labels_[key]; + } const strokeState = strokeKey ? this.strokeStates[strokeKey] : null; const fillState = fillKey ? this.fillStates[fillKey] : null; const textState = this.textStates[textKey]; @@ -239,6 +249,7 @@ class Executor { contextInstructions.push('fillText', [lines[i], x + leftRight * widths[i], 0.5 * (strokeWidth + lineHeight) + i * lineHeight]); } } + this.labels_[key] = label; return label; }