JS array数组拼接 push() concat() 的方法效率对比
程序开发
2023-09-02 17:02:07
在做词条处理工具的时候,遇到了这个问题,在使用 concat() 拼接数组的时候要比 push() 拼接耗时多9倍
let words = []
let needToBeAddedArray = [] // 需要拼接到 words 的数组
使用 concat() 的耗时 6081ms
words = words.concat(currentWords) // 拼接词组

使用 push() 的耗时 56ms
words.push(...currentWords) // 拼接词组

总结
所以使用 array.push(...otherArray) 的方式是最高效的
标签:
相关文章
-
无相关信息
