关于javascript:js-array数组拼接-push-concat-的方法效率对比差10倍

1次阅读

共计 279 个字符,预计需要花费 1 分钟才能阅读完成。

js array 数组拼接 push() concat() 的办法效率比照

在做词条解决工具的时候,遇到了这个问题,在应用 concat() 拼接数组的时候要比 push() 拼接耗时多 9 倍

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

应用 concat() 的耗时 6081ms

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

应用 push() 的耗时 56ms

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

总结

所以应用 array.push(...otherArray) 的形式是最高效的

正文完
 0