Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/normalize-color/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

'use strict';

const cachedColors = new Map();

function normalizeColor(color) {
if (typeof color === 'number') {
if (color >>> 0 === color && color >= 0 && color <= 0xffffffff) {
Expand All @@ -24,6 +26,23 @@ function normalizeColor(color) {
return null;
}

if (cachedColors.has(color)) {
// Map iteration order follows insertion order, so re-inserting on every
// hit keeps the least-recently-used entry first.
const cachedColor = cachedColors.get(color);
cachedColors.delete(color);
cachedColors.set(color, cachedColor);
return cachedColor;
}
const normalizedColor = parseColorString(color);
if (cachedColors.size >= 1024) {
cachedColors.delete(cachedColors.keys().next().value);
}
Comment thread
riteshshukla04 marked this conversation as resolved.
cachedColors.set(color, normalizedColor);
return normalizedColor;
}

function parseColorString(color) {
const matchers = getMatchers();
let match;

Expand Down
Loading