Skip to content

Commit d823844

Browse files
authored
fix: add caching to color visualization calculation (#5851)
1 parent 4facf90 commit d823844

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/visualization/utils/colorMappingUtils.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ export const generateSeriesToColorHex = (
155155
columnGroupMap,
156156
properties: XYViewProperties
157157
) => {
158+
const cache = {}
158159
const seriesToColorHex = {}
159-
const cgMap = {...columnGroupMap}
160-
cgMap.mappings.forEach((graphLine, colorIndex) => {
160+
columnGroupMap.mappings.forEach((graphLine, colorIndex) => {
161161
const id = getSeriesId(graphLine, columnGroupMap.columnKeys)
162162
let colors
163163
if (Array.isArray(properties.colors) && properties.colors.length) {
@@ -168,9 +168,21 @@ export const generateSeriesToColorHex = (
168168
colors = DEFAULT_LINE_COLORS
169169
}
170170
const colorsHexArray = colors.map(value => value.hex)
171-
const fillScale = getNominalColorScale(columnGroupMap, colorsHexArray)
171+
const key = colorsHexArray.join('')
172+
let fillScale
173+
if (cache[key]) {
174+
fillScale = cache[key]
175+
} else {
176+
/**
177+
* this is an expensive operation to perform, so we want to cache the result and use the returned function
178+
* Since the size of the columnGroupMap is the same in the context of each function, we can rely upon the
179+
* colorsHexArray as our unique key identifier and rely upon that for caching the reuslts
180+
*/
181+
fillScale = getNominalColorScale(columnGroupMap, colorsHexArray)
182+
cache[key] = fillScale
183+
}
172184
seriesToColorHex[id] = fillScale(colorIndex)
173185
})
174186

175-
return {...seriesToColorHex}
187+
return seriesToColorHex
176188
}

0 commit comments

Comments
 (0)