Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should use a loop-based technique. Other answers on this page that are based on using <code>.apply</code> can fail for large arrays. </p> <p>A fairly terse loop-based implementation is:</p> <pre><code>Array.prototype.extend = function (other_array) { /* you should include a test to check whether other_array really is an array */ other_array.forEach(function(v) {this.push(v)}, this); } </code></pre> <p>You can then do the following:</p> <pre><code>var a = [1,2,3]; var b = [5,4,3]; a.extend(b); </code></pre> <p><a href="https://stackoverflow.com/a/1374131/1280629">DzinX's answer</a> (using push.apply) and other <code>.apply</code> based methods fail when the array that we are appending is large (tests show that for me large is > 150000 entries approx in Chrome, and > 500000 entries in Firefox). You can see this error occurring in <a href="http://jsperf.com/array-extending-push-vs-concat" rel="noreferrer">this jsperf</a>.</p> <p>An error occurs because the call stack size is exceeded when 'Function.prototype.apply' is called with a large array as the second argument. (The MDN has a note on the dangers of exceeding call stack size using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply" rel="noreferrer">Function.prototype.apply</a> - see the section titled "apply and built-in functions")</p> <p>For a speed comparison with other answers on this page check out <a href="http://jsperf.com/array-extending-push-vs-concat/18" rel="noreferrer">this jsperf</a> (thanks to EaterOfCode). The loop-based implementation is similar in speed to using Array.push.apply, but tends to be a little slower than Array.slice.apply.</p> <p>Interestingly, if the array you are appending is sparse, the <code>forEach</code> based method above can take advantage of the sparsity and outperform the <code>.apply</code> based methods, check out <a href="http://jsperf.com/array-extending-push-vs-concat/5" rel="noreferrer">this jsperf</a> if you want to test this for yourself. </p> <p>By the way, do not be tempted (as I was!) to further shorten the forEach implementation to:</p> <pre><code>Array.prototype.extend = function (array) { array.forEach(this.push, this); } </code></pre> <p>because this produces garbage results! Why? Because Array.prototype.forEach provides 3 arguments to the function it calls - these are: (element_value, element_index, source_array). All of these will be pushed onto your first array for every iteration of forEach if you use "forEach(this.push, this)"!</p>
    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. 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