Commit 041fd5f2 authored by John Resig's avatar John Resig

Reduced the number of function calls required for .find() (single element...

Reduced the number of function calls required for .find() (single element root), optimized some calls to jQuery() as well. Goes towards fixing #4240.
parent 9c0ddfa2
...@@ -22,7 +22,16 @@ var ...@@ -22,7 +22,16 @@ var
jQuery.fn = jQuery.prototype = { jQuery.fn = jQuery.prototype = {
init: function( selector, context ) { init: function( selector, context ) {
// Make sure that a selection was provided // Make sure that a selection was provided
selector = selector || document; if ( selector === undefined ) {
selector = document;
}
// Handle "", null
if ( !selector ) {
this.length = 0;
this.context = document;
return this;
}
// Handle $(DOMElement) // Handle $(DOMElement)
if ( selector.nodeType ) { if ( selector.nodeType ) {
...@@ -31,6 +40,7 @@ jQuery.fn = jQuery.prototype = { ...@@ -31,6 +40,7 @@ jQuery.fn = jQuery.prototype = {
this.context = selector; this.context = selector;
return this; return this;
} }
// Handle HTML strings // Handle HTML strings
if ( typeof selector === "string" ) { if ( typeof selector === "string" ) {
// Are we dealing with HTML string or an ID? // Are we dealing with HTML string or an ID?
...@@ -40,17 +50,18 @@ jQuery.fn = jQuery.prototype = { ...@@ -40,17 +50,18 @@ jQuery.fn = jQuery.prototype = {
if ( match && (match[1] || !context) ) { if ( match && (match[1] || !context) ) {
// HANDLE: $(html) -> $(array) // HANDLE: $(html) -> $(array)
if ( match[1] ) if ( match[1] ) {
selector = jQuery.clean( [ match[1] ], context ); selector = jQuery.clean( [ match[1] ], context );
// HANDLE: $("#id") // HANDLE: $("#id")
else { } else {
var elem = document.getElementById( match[3] ); var elem = document.getElementById( match[3] );
// Handle the case where IE and Opera return items // Handle the case where IE and Opera return items
// by name instead of ID // by name instead of ID
if ( elem && elem.id != match[3] ) if ( elem && elem.id != match[3] ) {
return jQuery().find( selector ); return jQuery().find( selector );
}
// Otherwise, we inject the element directly into the jQuery object // Otherwise, we inject the element directly into the jQuery object
var ret = jQuery( elem || [] ); var ret = jQuery( elem || [] );
...@@ -61,13 +72,21 @@ jQuery.fn = jQuery.prototype = { ...@@ -61,13 +72,21 @@ jQuery.fn = jQuery.prototype = {
// HANDLE: $(expr, [context]) // HANDLE: $(expr, [context])
// (which is just equivalent to: $(content).find(expr) // (which is just equivalent to: $(content).find(expr)
} else } else if ( !context || context.nodeType ) {
return jQuery( context ).find( selector ); this[0] = context || document;
this.length = 1;
this.context = context;
return this.find( selector );
} else {
return (context.jquery ? context : jQuery( context )).find( selector );
}
// HANDLE: $(function) // HANDLE: $(function)
// Shortcut for document ready // Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) } else if ( jQuery.isFunction( selector ) ) {
return jQuery( document ).ready( selector ); return jQuery( document ).ready( selector );
}
// Make sure that old selector state is passed along // Make sure that old selector state is passed along
if ( selector.selector && selector.context ) { if ( selector.selector && selector.context ) {
...@@ -274,8 +293,7 @@ jQuery.fn = jQuery.prototype = { ...@@ -274,8 +293,7 @@ jQuery.fn = jQuery.prototype = {
find: function( selector ) { find: function( selector ) {
if ( this.length === 1 ) { if ( this.length === 1 ) {
var ret = this.pushStack( [], "find", selector ); var ret = this.pushStack( "", "find", selector );
ret.length = 0;
jQuery.find( selector, this[0], ret ); jQuery.find( selector, this[0], ret );
return ret; return ret;
} else { } else {
......
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