Skip to content

Commit

Permalink
Rewrote .merge() (faster and less obtuse now). Fixed #5610.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeresig committed Dec 10, 2009
1 parent f25eedf commit 715d1c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
25 changes: 11 additions & 14 deletions src/core.js
Expand Up @@ -567,23 +567,20 @@ jQuery.extend({
}, },


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


// We have to get length this way when IE & Opera overwrite the length if ( typeof second.length === "number" ) {
// expando of getElementsByTagName for ( var l = second.length; j < l; j++ ) {
if ( i && i.nodeType ) { first[ i++ ] = second[ j ];
for ( i = 0; second[i]; ++i ) {} }
} } else {

while ( second[j] !== undefined ) {
pos = i + first.length; first[ i++ ] = second[ j++ ];

}
// Correct length for non Arrays
first.length = pos;

while ( i ) {
first[ --pos ] = second[ --i ];
} }


first.length = i;

return first; return first;
}, },


Expand Down
6 changes: 5 additions & 1 deletion test/unit/core.js
Expand Up @@ -711,7 +711,7 @@ test("jQuery.each(Object,Function)", function() {
}); });


test("jQuery.makeArray", function(){ test("jQuery.makeArray", function(){
expect(15); expect(17);


equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" ); equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );


Expand Down Expand Up @@ -744,6 +744,10 @@ test("jQuery.makeArray", function(){
equals( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" ); equals( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );


ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" ); ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" );

// For #5610
same( jQuery.makeArray({'length': '0'}), [], "Make sure object is coerced properly.");
same( jQuery.makeArray({'length': '5'}), [], "Make sure object is coerced properly.");
}); });


test("jQuery.isEmptyObject", function(){ test("jQuery.isEmptyObject", function(){
Expand Down

1 comment on commit 715d1c5

@rkatic
Copy link
Contributor

@rkatic rkatic commented on 715d1c5 Dec 10, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, your version is faster only in the special (very rare) case of overwritted length property. In all other cases, my one was little faster.
Also you are not considering the case obj.length = new Number(N) - but yeah it is not so relevant I suppose.

Please sign in to comment.