Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are at least <strong>5</strong> (!) ways to clone an array:</p> <ul> <li>loop</li> <li>slice</li> <li>Array.from()</li> <li>concat</li> <li>spread operator, etc. <em>(so slow, forget it for now)</em> </li> </ul> <p>There has been a huuuge <a href="http://jsben.ch/#/wQ9RU" rel="noreferrer">BENCHMARKS thread</a>, providing following information:</p> <ul> <li><p>for <strong>blink</strong> browsers <code>slice()</code> is the fastest method, <code>concat()</code> is a bit slower, and <code>while loop</code> is 2.4x slower. </p></li> <li><p>for other browsers <code>while loop</code> is the fastest method, since those browsers don't have internal optimizations for <code>slice</code> and <code>concat</code>.</p></li> </ul> <p>This remains true in Jul 2016. </p> <p>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.</p> <p><strong>while loop</strong></p> <pre><code>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); </code></pre> <p><strong>slice</strong> </p> <pre><code>n = 1000*1000; start = + new Date(); a = Array(n); b = a.slice(); console.log(new Date() - start); </code></pre> <p>Please note that these methods will clone the Array object itself, array contents however are copied by reference and are not deep cloned.</p> <pre><code>origAr == clonedArr //returns false origAr[0] == clonedArr[0] //returns true </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload