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

Commit 6994010

Browse files
authored
Deprecated: Improve $.trim performance for strings with lots of whitespace
Regex imp implementation takes `O(N^2)` time to trim the string when multiple adjacent spaces were present. The new expression require that the "whitespace run" starts from a non-whitespace to avoid `O(N^2)` behavior when the engine would try matching `\s+$` at each space position. Closes gh-5068
1 parent 410d5cf commit 6994010

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/deprecated.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ define( [
1515

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

2022
// Bind a function to a context, optionally partially applying any
2123
// arguments.
@@ -82,6 +84,6 @@ jQuery.isNumeric = function( obj ) {
8284
jQuery.trim = function( text ) {
8385
return text == null ?
8486
"" :
85-
( text + "" ).replace( rtrim, "" );
87+
( text + "" ).replace( rtrim, "$1" );
8688
};
8789
} );

0 commit comments

Comments
 (0)