Skip to content

[charts] Use Map for string cache instead of object #17982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2025
Merged
Show file tree
Hide file tree
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
29 changes: 7 additions & 22 deletions packages/x-charts/src/internals/domUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,8 @@ function isSsr(): boolean {
return typeof window === 'undefined';
}

interface StringCache {
widthCache: Record<string, { width: number; height: number }>;
cacheCount: number;
}
const stringCache = new Map<string, { width: number; height: number }>();

const stringCache: StringCache = {
widthCache: {},
cacheCount: 0,
};
const MAX_CACHE_NUM = 2000;
const SPAN_STYLE = {
position: 'absolute',
Expand Down Expand Up @@ -114,8 +107,9 @@ export const getStringSize = (text: string | number, style: React.CSSProperties
const styleString = getStyleString(style);
const cacheKey = `${str}-${styleString}`;

if (stringCache.widthCache[cacheKey]) {
return stringCache.widthCache[cacheKey];
const size = stringCache.get(cacheKey);
if (size) {
return size;
}

try {
Expand All @@ -139,13 +133,10 @@ export const getStringSize = (text: string | number, style: React.CSSProperties
const rect = measurementSpan.getBoundingClientRect();
const result = { width: rect.width, height: rect.height };

stringCache.widthCache[cacheKey] = result;
stringCache.set(cacheKey, result);

if (stringCache.cacheCount + 1 > MAX_CACHE_NUM) {
stringCache.cacheCount = 0;
stringCache.widthCache = {};
} else {
stringCache.cacheCount += 1;
if (stringCache.size + 1 > MAX_CACHE_NUM) {
stringCache.clear();
}

if (process.env.NODE_ENV === 'test') {
Expand All @@ -166,9 +157,3 @@ export const getStringSize = (text: string | number, style: React.CSSProperties
return { width: 0, height: 0 };
}
};

// eslint-disable-next-line @typescript-eslint/naming-convention
export function unstable_cleanupDOM() {
// const measurementSpan = document.getElementById(MEASUREMENT_SPAN_ID);
// measurementSpan?.remove();
}
1 change: 0 additions & 1 deletion packages/x-charts/src/internals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export * from './configInit';
export * from './getLabel';
export * from './getSVGPoint';
export * from './isDefined';
export { unstable_cleanupDOM } from './domUtils';
export * from './getScale';
export * from './stackSeries';
export * from './getCurve';
Expand Down
2 changes: 0 additions & 2 deletions test/setupVitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import sinon from 'sinon';
import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingDataGrid } from '@mui/x-data-grid';
import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingDataGridPro } from '@mui/x-data-grid-pro';
import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingTreeView } from '@mui/x-tree-view';
import { unstable_cleanupDOM as unstable_cleanupDOMCharts } from '@mui/x-charts/internals';
import failOnConsole from 'vitest-fail-on-console';
import { clearWarningsCache } from '@mui/x-internals/warning';
import { isJSDOM } from './utils/skipIf';
Expand Down Expand Up @@ -38,7 +37,6 @@ afterEach(() => {
unstable_resetCleanupTrackingDataGrid();
unstable_resetCleanupTrackingDataGridPro();
unstable_resetCleanupTrackingTreeView();
unstable_cleanupDOMCharts();

// Restore Sinon default sandbox to avoid memory leak
// See https://github.com/sinonjs/sinon/issues/1866
Expand Down
2 changes: 0 additions & 2 deletions test/utils/mochaHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { restore } from 'sinon';
import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingDataGrid } from '@mui/x-data-grid';
import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingDataGridPro } from '@mui/x-data-grid-pro';
import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingTreeView } from '@mui/x-tree-view';
import { unstable_cleanupDOM as unstable_cleanupDOMCharts } from '@mui/x-charts/internals';
import { clearWarningsCache } from '@mui/x-internals/warning';
import { generateTestLicenseKey, setupTestLicenseKey } from './testLicense';

Expand Down Expand Up @@ -40,7 +39,6 @@ export function createXMochaHooks(coreMochaHooks = {}) {
unstable_resetCleanupTrackingDataGrid();
unstable_resetCleanupTrackingDataGridPro();
unstable_resetCleanupTrackingTreeView();
unstable_cleanupDOMCharts();

// Restore Sinon default sandbox to avoid memory leak
// See https://github.com/sinonjs/sinon/issues/1866
Expand Down