Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference bewteen jQuery.merge() and JavaScript native function concat()?

I want to concatenate two arrays into one. I have found two functions can do the job; one is jQuery.merge() and the other one is the JavaScript built in function concat(). They seem to do the same thing, and I found this. It says: "Merge creates a smaller footprint because it loops through the original array and adds the new items. Concat is a built-in Javascript function and should be faster, but has a larger footprint." I am not sure if this statement is true, and are there any other differences?

like image 848
tmaster Avatar asked Feb 01 '13 16:02

tmaster


People also ask

What is the difference between concat and push in Javascript?

concat returns a new array while push returns the length of updated array. Because concat creates a new array to hold the result of merged arrays, it's slower than push . For small arrays, both methods do not produce a significant difference in term of performance.

What is the difference between Myarray push element and Myarray concat element?

The push() adds elements to the end of an array and returns the new length of the array. Thus your return here is invalid. The concat() method is used to merge arrays. Concat does not change the existing arrays, but instead returns a new array.

How to concat array in jQuery?

The jQuery merge() method together merges the content of two arrays into the first array. This method returns the merged array. The merge() method forms an array containing the elements of both arrays. If we require the first array, we should copy it before calling the merge() method.

How to merge 2 arrays in js?

The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays. The concat() method does not change the existing arrays.


2 Answers

That quote is right and the two functions do not actually do the same thing, one merges two arrays into the one (first param), and the second builds a new array from both.

The "footprint" that it is referring to, is the max amount of memory that will be in use at any time. Since merge, is only going to duplicate the second array, it should use less memory, since at any time, it will only need to have 2 arrays in memory.

The built in function will need to have 3 arrays, and the new array will have to be the size of both arrays that are being concatenated.

Assuming 1000 elements in array A and B, and the merged array as C:

  • Merge would need 1000+1000[a->c], and 1000[b] or 3000 spots.
  • Concat would need 1000+1000[c] and 1000[a] and 1000[b] or 4000 spots.

Less memory could be faster if there was a lot of memory in use, less moves could also be faster.

Some tests I found

like image 66
nycynik Avatar answered Oct 23 '22 18:10

nycynik


In tmaster's situation, he used jQuery.merge() to merge 2 arrays.Actually,JQuery.merge can used to merge jQuery Object(jQuery Object has length prop too) with Array.Here is jQuery.merge()'s code:

merge: function( first, second ) {
    var len = +second.length,
        j = 0,
        i = first.length;

    for ( ; j < len; j++ ) {
        first[ i++ ] = second[ j ];
    }

    first.length = i;

    return first;
},

So for JQuery object has length property,it can use merge to copy the second JQuery Object attr like 0,1 to the first JQuery Object .Here is a example :

    pushStack: function( elems ) {

    // Build a new jQuery matched element set
    var ret = jQuery.merge( this.constructor(), elems );

    ...

    return ret;
},

this.constructor() return a jQuery Object,elems too. But concat can't,that can only used with Array.This is diff between concat and JQuery.merge,too.

like image 27
jinspire Avatar answered Oct 23 '22 18:10

jinspire