]> source.dussan.org Git - jquery.git/commitdiff
Ref #14313: Optimize 1.x jQuery.merge for size.
authorAmey Sakhadeo <me@ameyms.com>
Wed, 28 Aug 2013 16:54:55 +0000 (22:24 +0530)
committerRichard Gibson <richard.gibson@gmail.com>
Tue, 3 Sep 2013 06:02:45 +0000 (02:02 -0400)
(cherry picked from commits c75c9a8ebb5d2483daa68bec799e0d770083a811 f66d53c85643e703a8e37d9ca9375dd386a60cda)

src/core.js
test/unit/core.js

index d1e7a97e558b60b73d6b52c03dcc9518f03a5a52..cc13f1ed31fb8c3625578e91f73d4a8b7ce2d223 100644 (file)
@@ -629,15 +629,17 @@ jQuery.extend({
        },
 
        merge: function( first, second ) {
-               var l = second.length,
-                       i = first.length,
-                       j = 0;
+               var len = +second.length,
+                       j = 0,
+                       i = first.length;
 
-               if ( typeof l === "number" ) {
-                       for ( ; j < l; j++ ) {
-                               first[ i++ ] = second[ j ];
-                       }
-               } else {
+               while ( j < len ) {
+                       first[ i++ ] = second[ j++ ];
+               }
+
+               // Support: IE<9
+               // Workaround non-numeric length overrides of otherwise arraylike objects (e.g., NodeLists)
+               if ( len !== len ) {
                        while ( second[j] !== undefined ) {
                                first[ i++ ] = second[ j++ ];
                        }
index 8817c95f9edc774108eb5450f4fa9d57c4675156..e57d5333478b00122c3a6807362631cd0adde2ff 100644 (file)
@@ -905,25 +905,65 @@ test("jQuery.map", function() {
 });
 
 test("jQuery.merge()", function() {
-       expect(8);
+       expect( 10 );
 
-       deepEqual( jQuery.merge([],[]), [], "Empty arrays" );
+       deepEqual(
+               jQuery.merge( [], [] ),
+               [],
+               "Empty arrays"
+       );
 
-       deepEqual( jQuery.merge([ 1 ],[ 2 ]), [ 1, 2 ], "Basic" );
-       deepEqual( jQuery.merge([ 1, 2 ], [ 3, 4 ]), [ 1, 2, 3, 4 ], "Basic" );
+       deepEqual(
+               jQuery.merge( [ 1 ], [ 2 ] ),
+               [ 1, 2 ],
+               "Basic (single-element)"
+       );
+       deepEqual(
+               jQuery.merge( [ 1, 2 ], [ 3, 4 ] ),
+               [ 1, 2, 3, 4 ],
+               "Basic (multiple-element)"
+       );
 
-       deepEqual( jQuery.merge([ 1, 2 ],[]), [ 1, 2 ], "Second empty" );
-       deepEqual( jQuery.merge([],[ 1, 2 ]), [ 1, 2 ], "First empty" );
+       deepEqual(
+               jQuery.merge( [ 1, 2 ], [] ),
+               [ 1, 2 ],
+               "Second empty"
+       );
+       deepEqual(
+               jQuery.merge( [], [ 1, 2 ] ),
+               [ 1, 2 ],
+               "First empty"
+       );
 
        // Fixed at [5998], #3641
-       deepEqual( jQuery.merge([ -2, -1 ], [ 0, 1, 2 ]), [ -2, -1 , 0, 1, 2 ],
-               "Second array including a zero (falsy)");
+       deepEqual(
+               jQuery.merge( [ -2, -1 ], [ 0, 1, 2 ] ),
+               [ -2, -1 , 0, 1, 2 ],
+               "Second array including a zero (falsy)"
+       );
 
        // After fixing #5527
-       deepEqual( jQuery.merge([], [ null, undefined ]), [ null, undefined ],
-               "Second array including null and undefined values");
-       deepEqual( jQuery.merge({ length: 0 }, [ 1, 2 ] ), { length: 2, 0: 1, 1: 2},
-               "First array like");
+       deepEqual(
+               jQuery.merge( [], [ null, undefined ] ),
+               [ null, undefined ],
+               "Second array including null and undefined values"
+       );
+       deepEqual(
+               jQuery.merge( { length: 0 }, [ 1, 2 ] ),
+               { length: 2, 0: 1, 1: 2 },
+               "First array like"
+       );
+       deepEqual(
+               jQuery.merge( [ 1, 2 ], { length: 1, 0: 3 } ),
+               [ 1, 2, 3 ],
+               "Second array like"
+       );
+
+       deepEqual(
+               jQuery.merge( [], document.getElementById("lengthtest").getElementsByTagName("input") ),
+               [ document.getElementById("length"), document.getElementById("idTest") ],
+               "Second NodeList"
+       );
 });
 
 test("jQuery.extend(Object, Object)", function() {