From bf99b3e30c72b9133530e745ccd5580b3f2560fe Mon Sep 17 00:00:00 2001 From: rogerleung0411 Date: Thu, 23 May 2024 17:32:37 +0800 Subject: [PATCH] perf: reduce InputTag time complexity --- components/InputTag/input-tag.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/InputTag/input-tag.tsx b/components/InputTag/input-tag.tsx index 94400e0c0f..43332d2b04 100644 --- a/components/InputTag/input-tag.tsx +++ b/components/InputTag/input-tag.tsx @@ -372,9 +372,13 @@ function InputTag(baseProps: InputTagProps, ref) { // CSSTransition needs to be a direct child of TransitionGroup, otherwise the animation will NOT work // https://github.com/arco-design/arco-design/issues/622 const childrenTagWithAnimation = useMemo(() => { + // a map for duplication check, avoid O(n^2) complexity. + const valueCountMap: Record = {}; const items = value.map((x, i) => { // Check whether two tags have same value. If so, set different key for them to avoid only rendering one tag. - const isRepeat = value.findIndex((item) => item.value === x.value) !== i; + const valueCount = valueCountMap[x.value] || 0; + const isRepeat = valueCount >= 1; + valueCountMap[x.value] = valueCount + 1; const { dom: eleTag, valueKey } = mergedRenderTag(x, i); return React.isValidElement(eleTag) ? (