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

后端 未结 22 1482
佛祖请我去吃肉
佛祖请我去吃肉 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:46

    There is a much cleaner solution:

    var srcArray = [1, 2, 3];
    var clonedArray = srcArray.length === 1 ? [srcArray[0]] : Array.apply(this, srcArray);
    

    The length check is required, because the Array constructor behaves differently when it is called with exactly one argument.

    0 讨论(0)
  • 2020-11-22 02:47

    There are at least 6 (!) ways to clone an array:

    • loop
    • slice
    • Array.from()
    • concat
    • spread operator (FASTEST)
    • map A.map(function(e){return e;});

    There has been a huuuge BENCHMARKS thread, providing following information:

    • for blink browsers slice() is the fastest method, concat() is a bit slower, and while loop is 2.4x slower.

    • for other browsers while loop is the fastest method, since those browsers don't have internal optimizations for slice and concat.

    This remains true in Jul 2016.

    Below are simple scripts that you can copy-paste into your browser's console and run several times to see the picture. They output milliseconds, lower is better.

    while loop

    n = 1000*1000;
    start = + new Date();
    a = Array(n); 
    b = Array(n); 
    i = a.length;
    while(i--) b[i] = a[i];
    console.log(new Date() - start);
    

    slice

    n = 1000*1000;
    start = + new Date();
    a = Array(n); 
    b = a.slice();
    console.log(new Date() - start);
    

    Please note that these methods will clone the Array object itself, array contents however are copied by reference and are not deep cloned.

    origAr == clonedArr //returns false
    origAr[0] == clonedArr[0] //returns true
    
    0 讨论(0)
  • 2020-11-22 02:47

    As @Dan said "This answer becomes outdated fast. Use benchmarks to check the actual situation", there is one specific answer from jsperf that has not had an answer for itself: while:

    var i = a.length;
    while(i--) { b[i] = a[i]; }
    

    had 960,589 ops/sec with the runnerup a.concat() at 578,129 ops/sec, which is 60%.

    This is the lastest Firefox (40) 64 bit.


    @aleclarson created a new, more reliable benchmark.

    0 讨论(0)
  • 2020-11-22 02:48
    var cloned_array = [].concat(target_array);
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 02:50

    Fast ways to duplicate an array in JavaScript in Order:

    #1: array1copy = [...array1];

    #2: array1copy = array1.slice(0);

    #3: array1copy = array1.slice();

    If your array objects contain some JSON-non-serializable content (functions, Number.POSITIVE_INFINITY, etc.) better to use

    array1copy = JSON.parse(JSON.stringify(array1))

    0 讨论(0)
提交回复
热议问题