Fastest way to duplicate an array in JavaScript - slice vs. 'for' loop

后端 未结 22 2107
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 02:10

In order to duplicate an array in JavaScript: which of the following is faster to use?

###Slice method

var dup_array = original_array.slice();
<         


        
22条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-22 02:50

    Technically slice is the fastest way. However, it is even faster if you add the 0 begin index.

    myArray.slice(0);
    

    is faster than

    myArray.slice();
    

    http://jsperf.com/cloning-arrays/3

提交回复
热议问题