X Tutup
Skip to content

Commit b6d920c

Browse files
committed
Fix for a selector speed regression (calling a simple selector many times resulted in a significant speed down). This has been fixed by breaking the RegExps out into the global scope. This required that a closure be implemented around the full jQuery script (which is now the case). Some simple changes were made in addition to the RegExp one, allowing for some greater flexibility on our part - and hopefully better compression.
Speed results: http://dev.jquery.com/~john/ticket/1351/ vs. http://dev.jquery.com/~john/ticket/1351/113.html vs. http://dev.jquery.com/~john/ticket/1351/112.html
1 parent 8c15e85 commit b6d920c

File tree

5 files changed

+40
-39
lines changed

5 files changed

+40
-39
lines changed

src/event/event.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,6 @@ jQuery.extend({
578578
}
579579
});
580580

581-
new function(){
582-
583581
/**
584582
* Bind a function to the scroll event of each matched element.
585583
*
@@ -976,5 +974,3 @@ new function(){
976974

977975
// A fallback to window.onload, that will always work
978976
jQuery.event.add( window, "load", jQuery.ready );
979-
980-
};

src/intro.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// prevent execution of jQuery if included more than once
2-
if(typeof window.jQuery == "undefined") {
2+
if(typeof window.jQuery == "undefined") (function(){

src/jquery/jquery.js

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
* $Rev$
1010
*/
1111

12-
// Global undefined variable
13-
window.undefined = window.undefined;
14-
1512
/**
1613
* Create a new jQuery Object
1714
*
@@ -22,7 +19,7 @@ window.undefined = window.undefined;
2219
* @param jQuery|Element|Array<Element> c context
2320
* @cat Core
2421
*/
25-
var jQuery = function(a,c) {
22+
window.jQuery = function(a,c) {
2623
// If the context is global, return a new object
2724
if ( window == this || !this.init )
2825
return new jQuery(a,c);
@@ -35,7 +32,7 @@ if ( typeof $ != "undefined" )
3532
jQuery._$ = $;
3633

3734
// Map the jQuery namespace to the '$' one
38-
var $ = jQuery;
35+
window.$ = jQuery;
3936

4037
/**
4138
* This function accepts a string containing a CSS or
@@ -1527,7 +1524,7 @@ jQuery.extend({
15271524
}
15281525

15291526
if (prop.match(/float/i))
1530-
prop = jQuery.styleFloat;
1527+
prop = styleFloat;
15311528

15321529
if (!force && elem.style[prop])
15331530
ret = elem.style[prop];
@@ -1940,29 +1937,31 @@ jQuery.extend({
19401937
* @type Boolean
19411938
* @cat JavaScript
19421939
*/
1943-
new function() {
1944-
var b = navigator.userAgent.toLowerCase();
1945-
1946-
// Figure out what browser is being used
1947-
jQuery.browser = {
1948-
version: (b.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
1949-
safari: /webkit/.test(b),
1950-
opera: /opera/.test(b),
1951-
msie: /msie/.test(b) && !/opera/.test(b),
1952-
mozilla: /mozilla/.test(b) && !/(compatible|webkit)/.test(b)
1953-
};
1940+
var userAgent = navigator.userAgent.toLowerCase();
1941+
1942+
// Figure out what browser is being used
1943+
jQuery.browser = {
1944+
version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
1945+
safari: /webkit/.test(userAgent),
1946+
opera: /opera/.test(userAgent),
1947+
msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
1948+
mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
1949+
};
19541950

1951+
var styleFloat = jQuery.browser.msie ? "styleFloat" : "cssFloat";
1952+
1953+
jQuery.extend({
19551954
// Check to see if the W3C box model is being used
1956-
jQuery.boxModel = !jQuery.browser.msie || document.compatMode == "CSS1Compat";
1957-
1958-
jQuery.styleFloat = jQuery.browser.msie ? "styleFloat" : "cssFloat";
1959-
1960-
jQuery.props = {
1955+
boxModel: !jQuery.browser.msie || document.compatMode == "CSS1Compat",
1956+
1957+
styleFloat: jQuery.browser.msie ? "styleFloat" : "cssFloat",
1958+
1959+
props: {
19611960
"for": "htmlFor",
19621961
"class": "className",
1963-
"float": jQuery.styleFloat,
1964-
cssFloat: jQuery.styleFloat,
1965-
styleFloat: jQuery.styleFloat,
1962+
"float": styleFloat,
1963+
cssFloat: styleFloat,
1964+
styleFloat: styleFloat,
19661965
innerHTML: "innerHTML",
19671966
className: "className",
19681967
value: "value",
@@ -1971,9 +1970,8 @@ new function() {
19711970
readonly: "readOnly",
19721971
selected: "selected",
19731972
maxlength: "maxLength"
1974-
};
1975-
1976-
};
1973+
}
1974+
});
19771975

19781976
/**
19791977
* Get a set of elements containing the unique parents of the matched

src/outro.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
}
1+
})();

src/selector/selector.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2+
var chars = jQuery.browser.safari && parseInt(jQuery.browser.version) < 417 ?
3+
"(?:[\\w*_-]|\\\\.)" :
4+
"(?:[\\w\u0128-\uFFFF*_-]|\\\\.)",
5+
quickChild = new RegExp("^[/>]\\s*(" + chars + "+)"),
6+
quickID = new RegExp("^(" + chars + "+)(#)(" + chars + "+)"),
7+
quickClass = new RegExp("^([#.]?)(" + chars + "*)");
8+
19
jQuery.extend({
210
expr: {
311
"": "m[2]=='*'||jQuery.nodeName(a,m[2])",
@@ -62,8 +70,7 @@ jQuery.extend({
6270
/^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/,
6371

6472
// Match: :even, :last-chlid, #id, .class
65-
new RegExp("^([:.#]*)(" +
66-
( jQuery.chars = jQuery.browser.safari && parseInt(jQuery.browser.version) < 417 ? "(?:[\\w*_-]|\\\\.)" : "(?:[\\w\u0128-\uFFFF*_-]|\\\\.)" ) + "+)")
73+
new RegExp("^([:.#]*)(" + chars + "+)")
6774
],
6875

6976
multiFilter: function( expr, elems, not ) {
@@ -125,7 +132,7 @@ jQuery.extend({
125132

126133
// An attempt at speeding up child selectors that
127134
// point to a specific element tag
128-
var re = new RegExp("^[/>]\\s*(" + jQuery.chars + "+)");
135+
var re = quickChild;
129136
var m = re.exec(t);
130137

131138
if ( m ) {
@@ -194,7 +201,7 @@ jQuery.extend({
194201

195202
} else {
196203
// Optimize for the case nodeName#idName
197-
var re2 = new RegExp("^(" + jQuery.chars + "+)(#)(" + jQuery.chars + "+)");
204+
var re2 = quickID;
198205
var m = re2.exec(t);
199206

200207
// Re-organize the results, so that they're consistent
@@ -204,7 +211,7 @@ jQuery.extend({
204211
} else {
205212
// Otherwise, do a traditional filter check for
206213
// ID, class, and element selectors
207-
re2 = new RegExp("^([#.]?)(" + jQuery.chars + "*)");
214+
re2 = quickClass;
208215
m = re2.exec(t);
209216
}
210217

0 commit comments

Comments
 (0)
X Tutup