mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-10-19 16:15:56 +00:00
perf(editor): avoid rebuilding id sets on every resize (#12180)
perf: avoid rebuilding simultaneously-updated id set per element on multi-element resize updateBoundElements() rebuilt a Set of all simultaneouslyUpdated ids on every call. Multi-element resize/rotate call it once per selected element with the same array, making each pointer move O(n^2). Cache the set per array in a WeakMap. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
974f054baf
commit
84e3f5a40c
@@ -1541,10 +1541,27 @@ const doesNeedUpdate = (
|
||||
);
|
||||
};
|
||||
|
||||
// PERF: callers updating many elements invoke `updateBoundElements()` for each
|
||||
// of them with the same `simultaneouslyUpdated` array, so cache the id set
|
||||
// per array to avoid rebuilding it on every call (O(n^2) on multi-element
|
||||
// transforms)
|
||||
const simultaneouslyUpdatedElementIdsCache = new WeakMap<
|
||||
readonly ExcalidrawElement[],
|
||||
ReadonlySet<ExcalidrawElement["id"]>
|
||||
>();
|
||||
|
||||
const getSimultaneouslyUpdatedElementIds = (
|
||||
simultaneouslyUpdated: readonly ExcalidrawElement[] | undefined,
|
||||
): Set<ExcalidrawElement["id"]> => {
|
||||
return new Set((simultaneouslyUpdated || []).map((element) => element.id));
|
||||
): ReadonlySet<ExcalidrawElement["id"]> => {
|
||||
if (!simultaneouslyUpdated) {
|
||||
return new Set();
|
||||
}
|
||||
let ids = simultaneouslyUpdatedElementIdsCache.get(simultaneouslyUpdated);
|
||||
if (!ids) {
|
||||
ids = new Set(simultaneouslyUpdated.map((element) => element.id));
|
||||
simultaneouslyUpdatedElementIdsCache.set(simultaneouslyUpdated, ids);
|
||||
}
|
||||
return ids;
|
||||
};
|
||||
|
||||
export const getHeadingForElbowArrowSnap = (
|
||||
|
||||
Reference in New Issue
Block a user