diff --git a/blocksuite/affine/gfx/text/src/element-renderer/utils.ts b/blocksuite/affine/gfx/text/src/element-renderer/utils.ts index 665972995..947ce26eb 100644 --- a/blocksuite/affine/gfx/text/src/element-renderer/utils.ts +++ b/blocksuite/affine/gfx/text/src/element-renderer/utils.ts @@ -399,23 +399,27 @@ export function parseTokens(text: string): string[] { } export const charWidth = (() => { - const cachedCharWidth: Record> = {}; + const cachedCharWidth = new Map>(); const calculate = (char: string, font: string) => { const ascii = char.charCodeAt(0); - if (!cachedCharWidth[font]) { - cachedCharWidth[font] = []; - } - if (!cachedCharWidth[font][ascii]) { - const width = getLineWidth(char, font); - cachedCharWidth[font][ascii] = width; + + let fontCache = cachedCharWidth.get(font); + if (!fontCache) { + fontCache = []; + cachedCharWidth.set(font, fontCache); } - return cachedCharWidth[font][ascii]; + if (fontCache[ascii] === undefined) { + const width = getLineWidth(char, font); + fontCache[ascii] = width; + } + + return fontCache[ascii]; }; const getCache = (font: string) => { - return cachedCharWidth[font]; + return cachedCharWidth.get(font); }; return { calculate,