js array array splicing push() concat() method efficiency comparison

I encountered this problem when I was working on the entry processing tool. When using concat() splicing array, it takes 9 times push()

let words = []
let needToBeAddedArray = [] // 需要拼接到 words 的数组

Using concat() takes 6081ms

words = words.concat(currentWords) // 拼接词组

2021-10-18 14 46 27.jpg

push() using 0616d1b8163ccd

words.push(...currentWords) // 拼接词组

2021-10-18 14 43 03.jpg

Summarize

So array.push(...otherArray) is the most efficient


KyleBing
659 声望18 粉丝

前端,喜欢 Javascript scss,喜欢做一些实用的小工具