Commit ceb5b4c9 authored by Amey Sakhadeo's avatar Amey Sakhadeo Committed by Richard Gibson

Ref #14313: Optimize 1.x jQuery.merge for size.

(cherry picked from commits c75c9a8e f66d53c8)
parent 5cd68683
...@@ -629,15 +629,17 @@ jQuery.extend({ ...@@ -629,15 +629,17 @@ jQuery.extend({
}, },
merge: function( first, second ) { merge: function( first, second ) {
var l = second.length, var len = +second.length,
i = first.length, j = 0,
j = 0; i = first.length;
if ( typeof l === "number" ) { while ( j < len ) {
for ( ; j < l; j++ ) { first[ i++ ] = second[ j++ ];
first[ i++ ] = second[ j ]; }
}
} else { // Support: IE<9
// Workaround non-numeric length overrides of otherwise arraylike objects (e.g., NodeLists)
if ( len !== len ) {
while ( second[j] !== undefined ) { while ( second[j] !== undefined ) {
first[ i++ ] = second[ j++ ]; first[ i++ ] = second[ j++ ];
} }
......
...@@ -905,25 +905,65 @@ test("jQuery.map", function() { ...@@ -905,25 +905,65 @@ test("jQuery.map", function() {
}); });
test("jQuery.merge()", 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(
deepEqual( jQuery.merge([ 1, 2 ], [ 3, 4 ]), [ 1, 2, 3, 4 ], "Basic" ); 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(
deepEqual( jQuery.merge([],[ 1, 2 ]), [ 1, 2 ], "First empty" ); jQuery.merge( [ 1, 2 ], [] ),
[ 1, 2 ],
"Second empty"
);
deepEqual(
jQuery.merge( [], [ 1, 2 ] ),
[ 1, 2 ],
"First empty"
);
// Fixed at [5998], #3641 // Fixed at [5998], #3641
deepEqual( jQuery.merge([ -2, -1 ], [ 0, 1, 2 ]), [ -2, -1 , 0, 1, 2 ], deepEqual(
"Second array including a zero (falsy)"); jQuery.merge( [ -2, -1 ], [ 0, 1, 2 ] ),
[ -2, -1 , 0, 1, 2 ],
"Second array including a zero (falsy)"
);
// After fixing #5527 // After fixing #5527
deepEqual( jQuery.merge([], [ null, undefined ]), [ null, undefined ], deepEqual(
"Second array including null and undefined values"); jQuery.merge( [], [ null, undefined ] ),
deepEqual( jQuery.merge({ length: 0 }, [ 1, 2 ] ), { length: 2, 0: 1, 1: 2}, [ null, undefined ],
"First array like"); "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() { test("jQuery.extend(Object, Object)", function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment