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

后端 未结 22 1536
佛祖请我去吃肉
佛祖请我去吃肉 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:29
            const arr = ['1', '2', '3'];
    
             // Old way
            const cloneArr = arr.slice();
    
            // ES6 way
            const cloneArrES6 = [...arr];
    
    // But problem with 3rd approach is that if you are using muti-dimensional 
     // array, then only first level is copied
    
            const nums = [
                  [1, 2], 
                  [10],
             ];
    
            const cloneNums = [...nums];
    
    // Let's change the first item in the first nested item in our cloned array.
    
            cloneNums[0][0] = '8';
    
            console.log(cloneNums);
               // [ [ '8', 2 ], [ 10 ], [ 300 ] ]
    
            // NOOooo, the original is also affected
            console.log(nums);
              // [ [ '8', 2 ], [ 10 ], [ 300 ] ]
    

    So, in order to avoid these scenarios to happen, use

            const arr = ['1', '2', '3'];
    
            const cloneArr = Array.from(arr);
    
    0 讨论(0)
  • 2020-11-22 02:32

    It depends on the browser. If you look in the blog post Array.prototype.slice vs manual array creation, there is a rough guide to performance of each:

    Results:

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

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

    If you want a REAL cloned object/array in JS with cloned references of all attributes and sub-objects:

    export function clone(arr) {
        return JSON.parse(JSON.stringify(arr))
    }
    

    ALL other operations do not create clones, because they just change the base address of the root element, not of the included objects.

    Except you traverse recursive through the object-tree.

    For a simple copy, these are OK. For storage address relevant operations I suggest (and in most all other cases, because this is fast!) to type convert into string and back in a complete new object.

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

    Easiest way to deep clone Array or Object:

    var dup_array = JSON.parse(JSON.stringify(original_array))
    
    0 讨论(0)
  • 2020-11-22 02:34

    Take a look at: link. It's not about speed, but comfort. Besides as you can see you can only use slice(0) on primitive types.

    To make an independent copy of an array rather than a copy of the refence to it, you can use the array slice method.

    Example:

    To make an independent copy of an array rather than a copy of the refence to it, you can use the array slice method.

    var oldArray = ["mip", "map", "mop"];
    var newArray = oldArray.slice();
    

    To copy or clone an object :

    function cloneObject(source) {
        for (i in source) {
            if (typeof source[i] == 'source') {
                this[i] = new cloneObject(source[i]);
            }
            else{
                this[i] = source[i];
      }
        }
    }
    
    var obj1= {bla:'blabla',foo:'foofoo',etc:'etc'};
    var obj2= new cloneObject(obj1);
    

    Source: link

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