]> source.dussan.org Git - jquery.git/commitdiff
Landing some minor perf optimization to jQuery().
authorjeresig <jeresig@gmail.com>
Thu, 28 Jan 2010 22:17:51 +0000 (17:17 -0500)
committerjeresig <jeresig@gmail.com>
Thu, 28 Jan 2010 22:17:51 +0000 (17:17 -0500)
src/core.js
test/unit/core.js

index 81b48318ce31c22f2fa208a3e6d3c2043cbfbd40..2271ceb53702158b5f863e357785653b372d2955 100644 (file)
@@ -69,6 +69,14 @@ jQuery.fn = jQuery.prototype = {
                        this.length = 1;
                        return this;
                }
+               
+               // The body element only exists once, optimize finding it
+               if ( selector === "body" && !context ) {
+                       this.context = this[0] = document.body;
+                       this.selector = "body";
+                       this.length = 1;
+                       return this;
+               }
 
                // Handle HTML strings
                if ( typeof selector === "string" ) {
@@ -99,7 +107,7 @@ jQuery.fn = jQuery.prototype = {
                                                ret = buildFragment( [ match[1] ], [ doc ] );
                                                selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
                                        }
-
+                                       
                                        return jQuery.merge( this, selector );
                                        
                                // HANDLE: $("#id")
@@ -128,6 +136,7 @@ jQuery.fn = jQuery.prototype = {
                                this.selector = selector;
                                this.context = document;
                                selector = document.getElementsByTagName( selector );
+                               return jQuery.merge( this, selector );
 
                        // HANDLE: $(expr, $(...))
                        } else if ( !context || context.jquery ) {
@@ -150,9 +159,7 @@ jQuery.fn = jQuery.prototype = {
                        this.context = selector.context;
                }
 
-               return jQuery.isArray( selector ) ?
-                       this.setArray( selector ) :
-                       jQuery.makeArray( selector, this );
+               return jQuery.makeArray( selector, this );
        },
 
        // Start with an empty selector
@@ -604,6 +611,7 @@ jQuery.extend({
                        for ( var l = second.length; j < l; j++ ) {
                                first[ i++ ] = second[ j ];
                        }
+               
                } else {
                        while ( second[j] !== undefined ) {
                                first[ i++ ] = second[ j++ ];
index 54ad982d24f5d44d9bf244c2776d73eec33519c9..4576ab8eb6a07e4baa2d480f02ecd5a2fb210409 100644 (file)
@@ -25,7 +25,7 @@ test("jQuery()", function() {
        equals( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" );
 
                // can actually yield more than one, when iframes are included, the window is an array as well
-       equals( 1, jQuery(window).length, "Correct number of elements generated for jQuery(window)" );
+       equals( jQuery(window).length, 1, "Correct number of elements generated for jQuery(window)" );
 
 
        var main = jQuery("#main");