🌐 AI搜索 & 代理 主页
Skip to content

Commit b8076a9

Browse files
committed
Landing some minor perf optimization to jQuery().
1 parent 0db207d commit b8076a9

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/core.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ jQuery.fn = jQuery.prototype = {
6969
this.length = 1;
7070
return this;
7171
}
72+
73+
// The body element only exists once, optimize finding it
74+
if ( selector === "body" && !context ) {
75+
this.context = this[0] = document.body;
76+
this.selector = "body";
77+
this.length = 1;
78+
return this;
79+
}
7280

7381
// Handle HTML strings
7482
if ( typeof selector === "string" ) {
@@ -99,7 +107,7 @@ jQuery.fn = jQuery.prototype = {
99107
ret = buildFragment( [ match[1] ], [ doc ] );
100108
selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
101109
}
102-
110+
103111
return jQuery.merge( this, selector );
104112

105113
// HANDLE: $("#id")
@@ -128,6 +136,7 @@ jQuery.fn = jQuery.prototype = {
128136
this.selector = selector;
129137
this.context = document;
130138
selector = document.getElementsByTagName( selector );
139+
return jQuery.merge( this, selector );
131140

132141
// HANDLE: $(expr, $(...))
133142
} else if ( !context || context.jquery ) {
@@ -150,9 +159,7 @@ jQuery.fn = jQuery.prototype = {
150159
this.context = selector.context;
151160
}
152161

153-
return jQuery.isArray( selector ) ?
154-
this.setArray( selector ) :
155-
jQuery.makeArray( selector, this );
162+
return jQuery.makeArray( selector, this );
156163
},
157164

158165
// Start with an empty selector
@@ -604,6 +611,7 @@ jQuery.extend({
604611
for ( var l = second.length; j < l; j++ ) {
605612
first[ i++ ] = second[ j ];
606613
}
614+
607615
} else {
608616
while ( second[j] !== undefined ) {
609617
first[ i++ ] = second[ j++ ];

test/unit/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test("jQuery()", function() {
2525
equals( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" );
2626

2727
// can actually yield more than one, when iframes are included, the window is an array as well
28-
equals( 1, jQuery(window).length, "Correct number of elements generated for jQuery(window)" );
28+
equals( jQuery(window).length, 1, "Correct number of elements generated for jQuery(window)" );
2929

3030

3131
var main = jQuery("#main");

0 commit comments

Comments
 (0)