From f8bb923c068417e65ba6a2ab975aa724002af0d6 Mon Sep 17 00:00:00 2001 From: Max Franz Date: Mon, 11 Nov 2024 11:52:16 -0500 Subject: [PATCH] Use a single cached value instead of growing indefinitely Pro: smaller, simpler Con: slower on graphs where you toggle styles Ref: Node label flickering when animating font-size #3270 --- .../renderer/base/coord-ele-math/labels.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/extensions/renderer/base/coord-ele-math/labels.js b/src/extensions/renderer/base/coord-ele-math/labels.js index 26ceb2935..bf97f5b8e 100644 --- a/src/extensions/renderer/base/coord-ele-math/labels.js +++ b/src/extensions/renderer/base/coord-ele-math/labels.js @@ -471,14 +471,11 @@ BRp.calculateLabelDimensions = function( ele, text ){ var document = containerWindow.document; - let cacheKey = util.hashString( text, ele._private.labelDimsKey ); + let cacheKey = util.hashString(text, ele._private.labelDimsKey); + let cache = r.labelDimCache || (r.labelDimCache = { key: null, value: null }); - let cache = r.labelDimCache || (r.labelDimCache = []); - - let existingVal = cache[ cacheKey ]; - - if( existingVal != null ){ - return existingVal; + if (cache.key === cacheKey) { + return cache.value; } let padding = 0; // add padding around text dims, as the measurement isn't that accurate @@ -522,10 +519,13 @@ BRp.calculateLabelDimensions = function( ele, text ){ width += padding; height += padding; - return ( cache[ cacheKey ] = { + cache.key = cacheKey; + cache.value = { width, height - } ); + }; + + return cache.value; };