2개의 map vs 1개의 reduce 속도 비교

2024. 3. 25. 10:15개발/토막난 상식

반응형
console.time()
            const updatedEntireKeyList = data.map((value, index) => {
                return index + 1
            })
            setEntireKeyList(updatedEntireKeyList)

            const keyUpdatedData = data.map((value, index) => ({
                ...value,
                key: index + 1,
            }))
            setKeySetData(keyUpdatedData)
            console.timeEnd()
console.time()
            const result = data.reduce(
                (acc, value, index) => {
                    const newItem = { ...value, key: index + 1 }
                    acc.keyUpdatedData.push(newItem)
                    acc.entireKeyList.push(index + 1)
                    return acc
                },
                { keyUpdatedData: [], entireKeyList: [] }
            )
            const { a, b } = result
            setEntireKeyList(a)
            setKeySetData(b)
            console.timeEnd()

 

 

결론: 



리듀스 승