Skip to content

Commit 3a55130

Browse files
committed
Change to more comprehensive serialiation memoization for colors
1 parent 854b6eb commit 3a55130

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/color/p5.Color.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import HSBSpace from './color_spaces/hsb.js';
3333
const map = (n, start1, stop1, start2, stop2) =>
3434
((n - start1) / (stop1 - start1) * (stop2 - start2) + start2);
3535

36-
const serializationMap = new WeakMap();
36+
const serializationMap = {};
3737

3838
class Color {
3939
// Reference to underlying color object depending on implementation
@@ -244,13 +244,14 @@ class Color {
244244
* </div>
245245
*/
246246
toString(format) {
247-
// NOTE: memoize more
248-
let colorString = serializationMap.get(this._color);
247+
const key = `${this._color.space.id}-${this._color.coords.join(",")}-${this._color.alpha}-${format}`;
248+
let colorString = serializationMap[key];
249+
249250
if(!colorString){
250251
colorString = serialize(this._color, {
251252
format
252253
});
253-
serializationMap.set(this._color, colorString);
254+
serializationMap[key] = colorString;
254255
}
255256
return colorString;
256257
}

0 commit comments

Comments
 (0)