Bug Tracker

Changeset 2821

Show
Ignore:
Timestamp:
08/21/07 08:25:11 (1 year ago)
Author:
jeresig
Message:

Using some of the ideas presented by rformato, I've significantly sped up $("#id") selection. It's now just 10% slower than doing: $(document.getElementById("test")), which seems quite acceptable. (Bug #1316)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/jquery/src/jquery/jquery.js

    r2819 r2821  
    3535window.$ = jQuery; 
    3636 
     37var quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#(\w+)$/; 
     38 
    3739/** 
    3840 * This function accepts a string containing a CSS or 
     
    151153        a = a || document; 
    152154 
     155        // Handle HTML strings 
     156        if ( typeof a  == "string" ) { 
     157            var m = quickExpr.exec(a); 
     158            if ( m && (m[1] || !c) ) { 
     159                // HANDLE: $(html) -> $(array) 
     160                if ( m[1] ) 
     161                    a = jQuery.clean( [ m[1] ] ); 
     162 
     163                // HANDLE: $("#id") 
     164                else { 
     165                    var tmp = document.getElementById( m[3] ); 
     166                    if ( tmp ) 
     167                        // Handle the case where IE and Opera return items 
     168                        // by name instead of ID 
     169                        if ( tmp.id != m[3] ) 
     170                            return jQuery().find( a ); 
     171                        else { 
     172                            this[0] = tmp; 
     173                            this.length = 1; 
     174                            return this; 
     175                        } 
     176                    else 
     177                        a = []; 
     178                } 
     179 
     180            // HANDLE: $(expr) 
     181            } else 
     182                return new jQuery( c ).find( a ); 
     183 
    153184        // HANDLE: $(function) 
    154185        // Shortcut for document ready 
    155         if ( jQuery.isFunction(a) ) 
     186        } else if ( jQuery.isFunction(a) ) 
    156187            return new jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ]( a ); 
    157  
    158         // Handle HTML strings 
    159         if ( typeof a  == "string" ) { 
    160             // HANDLE: $(html) -> $(array) 
    161             var m = /^[^<]*(<(.|\s)+>)[^>]*$/.exec(a); 
    162             if ( m ) 
    163                 a = jQuery.clean( [ m[1] ] ); 
    164  
    165             // HANDLE: $(expr) 
    166             else 
    167                 return new jQuery( c ).find( a ); 
    168         } 
    169188 
    170189        return this.setArray(