Merge pull request #20 from chanzuckerberg/bkmartinjr-perf2

Perf - cache parsed color descriptions
This commit is contained in:
Colin Megill
2018-05-08 10:38:23 -07:00
committed by GitHub
+16 -1
View File
@@ -1,7 +1,13 @@
// jshint esversion: 6
import { scaleRGB } from "./scaleRGB";
export const parseRGB = c => {
// maintain a cache of already parsed RGB names, as it is reasonably expensive
// to do this operation. This lets us have speed, but keep the pleasant ability
// to talk about colors by their text description eg, 'rgb(0,0,1)'
//
const colorCache = new Object(null); // no prototype
function parseColorName(c) {
if (c[0] !== "#") {
const _c = c.replace(/[^\d,.]/g, "").split(",");
return [scaleRGB(+_c[0]), scaleRGB(+_c[1]), scaleRGB(+_c[2])];
@@ -13,4 +19,13 @@ export const parseRGB = c => {
scaleRGB(parseInt(parsedHex[3], 16))
];
}
}
export const parseRGB = c => {
var cv = colorCache[c];
if (!cv) {
cv = parseColorName(c);
colorCache[c] = cv;
}
return cv;
};