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

Commit 159f9c1

Browse files
committed
Deprecated: use native String.trim instead of Regexp when possible
Regex imp implementation takes O(N^2) time to trim the string when multiple adjacent spaces were present. However, virtually all the modern browsers support String.trim properly, so we use it, and fallback to the old implementation for very old devices only.
1 parent 410d5cf commit 159f9c1

File tree

3 files changed

+76
-20
lines changed

3 files changed

+76
-20
lines changed

src/deprecated.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ define( [
66
"./var/isFunction",
77
"./var/isWindow",
88
"./var/slice",
9+
"./deprecated/support",
910

1011
"./deprecated/ajax-event-alias",
1112
"./deprecated/event"
12-
], function( jQuery, nodeName, camelCase, toType, isFunction, isWindow, slice ) {
13+
], function( jQuery, nodeName, camelCase, toType, isFunction, isWindow, slice, support ) {
1314

1415
"use strict";
1516

1617
// Support: Android <=4.0 only
1718
// Make sure we trim BOM and NBSP
18-
var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
19+
// Require that the "whitespace run" starts from a non-whitespace
20+
// to avoid O(N^2) behavior when the engine would try matching "\s$" at each space position.
21+
var rtrim = /^[\s\uFEFF\xA0]+|([^\s\uFEFF\xA0])[\s\uFEFF\xA0]+$/g;
1922

2023
// Bind a function to a context, optionally partially applying any
2124
// arguments.
@@ -80,8 +83,11 @@ jQuery.isNumeric = function( obj ) {
8083
};
8184

8285
jQuery.trim = function( text ) {
83-
return text == null ?
84-
"" :
85-
( text + "" ).replace( rtrim, "" );
86+
if ( text == null ) {
87+
return "";
88+
}
89+
return support.trim ?
90+
( text + "" ).trim() :
91+
( text + "" ).replace( rtrim, "$1" );
8692
};
8793
} );

src/deprecated/support.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
define( [
2+
"../var/support"
3+
], function( support ) {
4+
5+
"use strict";
6+
7+
support.trim = "" === "\uFEFF\xA0".trim();
8+
9+
return support;
10+
11+
} );

test/unit/support.js

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ testIframe(
7575
"radioValue": true,
7676
"reliableMarginLeft": true,
7777
"reliableTrDimensions": false,
78-
"scrollboxSize": true
78+
"scrollboxSize": true,
79+
"trim": true
7980
},
8081
ie_10_11: {
8182
"ajax": true,
@@ -94,7 +95,8 @@ testIframe(
9495
"radioValue": false,
9596
"reliableMarginLeft": true,
9697
"reliableTrDimensions": false,
97-
"scrollboxSize": true
98+
"scrollboxSize": true,
99+
"trim": true
98100
},
99101
ie_9: {
100102
"ajax": true,
@@ -113,7 +115,8 @@ testIframe(
113115
"radioValue": false,
114116
"reliableMarginLeft": true,
115117
"reliableTrDimensions": false,
116-
"scrollboxSize": false
118+
"scrollboxSize": false,
119+
"trim": true
117120
},
118121
chrome: {
119122
"ajax": true,
@@ -132,7 +135,8 @@ testIframe(
132135
"radioValue": true,
133136
"reliableMarginLeft": true,
134137
"reliableTrDimensions": true,
135-
"scrollboxSize": true
138+
"scrollboxSize": true,
139+
"trim": true
136140
},
137141
safari: {
138142
"ajax": true,
@@ -151,7 +155,8 @@ testIframe(
151155
"radioValue": true,
152156
"reliableMarginLeft": true,
153157
"reliableTrDimensions": true,
154-
"scrollboxSize": true
158+
"scrollboxSize": true,
159+
"trim": true
155160
},
156161
safari_9_10: {
157162
"ajax": true,
@@ -170,7 +175,8 @@ testIframe(
170175
"radioValue": true,
171176
"reliableMarginLeft": true,
172177
"reliableTrDimensions": true,
173-
"scrollboxSize": true
178+
"scrollboxSize": true,
179+
"trim": true
174180
},
175181
firefox: {
176182
"ajax": true,
@@ -189,7 +195,8 @@ testIframe(
189195
"radioValue": true,
190196
"reliableMarginLeft": true,
191197
"reliableTrDimensions": false,
192-
"scrollboxSize": true
198+
"scrollboxSize": true,
199+
"trim": true
193200
},
194201
firefox_60: {
195202
"ajax": true,
@@ -208,7 +215,8 @@ testIframe(
208215
"radioValue": true,
209216
"reliableMarginLeft": false,
210217
"reliableTrDimensions": true,
211-
"scrollboxSize": true
218+
"scrollboxSize": true,
219+
"trim": true
212220
},
213221
ios: {
214222
"ajax": true,
@@ -227,7 +235,8 @@ testIframe(
227235
"radioValue": true,
228236
"reliableMarginLeft": true,
229237
"reliableTrDimensions": true,
230-
"scrollboxSize": true
238+
"scrollboxSize": true,
239+
"trim": true
231240
},
232241
ios_9_10: {
233242
"ajax": true,
@@ -246,7 +255,8 @@ testIframe(
246255
"radioValue": true,
247256
"reliableMarginLeft": true,
248257
"reliableTrDimensions": true,
249-
"scrollboxSize": true
258+
"scrollboxSize": true,
259+
"trim": true
250260
},
251261
ios_8: {
252262
"ajax": true,
@@ -265,7 +275,8 @@ testIframe(
265275
"radioValue": true,
266276
"reliableMarginLeft": true,
267277
"reliableTrDimensions": true,
268-
"scrollboxSize": true
278+
"scrollboxSize": true,
279+
"trim": true
269280
},
270281
ios_7: {
271282
"ajax": true,
@@ -284,7 +295,8 @@ testIframe(
284295
"radioValue": true,
285296
"reliableMarginLeft": true,
286297
"reliableTrDimensions": true,
287-
"scrollboxSize": true
298+
"scrollboxSize": true,
299+
"trim": true
288300
},
289301
android: {
290302
"ajax": true,
@@ -303,16 +315,41 @@ testIframe(
303315
"radioValue": true,
304316
"reliableMarginLeft": false,
305317
"reliableTrDimensions": true,
306-
"scrollboxSize": true
318+
"scrollboxSize": true,
319+
"trim": true
320+
},
321+
android_4_0: {
322+
"ajax": true,
323+
"boxSizingReliable": true,
324+
"checkClone": false,
325+
"checkOn": false,
326+
"clearCloneStyle": true,
327+
"cors": true,
328+
"createHTMLDocument": true,
329+
"focusin": false,
330+
"noCloneChecked": true,
331+
"option": true,
332+
"optSelected": true,
333+
"pixelBoxStyles": false,
334+
"pixelPosition": false,
335+
"radioValue": true,
336+
"reliableMarginLeft": false,
337+
"reliableTrDimensions": true,
338+
"scrollboxSize": true,
339+
"trim": false
307340
}
308341
};
309342

310343
// Make the slim build pass tests.
311344
for ( browserKey in expectedMap ) {
312-
if ( !jQuery.ajax ) {
345+
if ( !includesModule( "ajax" ) ) {
313346
delete expectedMap[ browserKey ].ajax;
314347
delete expectedMap[ browserKey ].cors;
315348
}
349+
if ( !includesModule( "deprecated" ) ) {
350+
// If running no-deprecated test, then jQuery.trim does not exist, and we should not verify support for it.
351+
delete expectedMap[ browserKey ].trim;
352+
}
316353
}
317354

318355
if ( /edge\//i.test( userAgent ) ) {
@@ -332,8 +369,10 @@ testIframe(
332369
expected = expectedMap.firefox_60;
333370
} else if ( /firefox/i.test( userAgent ) ) {
334371
expected = expectedMap.firefox;
335-
} else if ( /android 4\.[0-3]/i.test( userAgent ) ) {
372+
} else if ( /android 4\.[1-3]/i.test( userAgent ) ) {
336373
expected = expectedMap.android;
374+
} else if ( /android 4\.0/i.test( userAgent ) ) {
375+
expected = expectedMap.android_4_0;
337376
} else if ( /iphone os (?:9|10)_/i.test( userAgent ) ) {
338377
expected = expectedMap.ios_9_10;
339378
} else if ( /iphone os 8_/i.test( userAgent ) ) {

0 commit comments

Comments
 (0)