Commit f84b0ba
committed
Performance: improve .trim performance for large strings contains lots of whitespaces
The old implementation took O(N^2) time to trim the string when multiple adjacent spaces
were present.
For instance, consider the string "A B" (10 spaces between A and B).
Then old regexp /[\s]+$/ would take 10 steps until it realizes
the regexp does not match at position 1.
Then it would try to match the regexp at position 2, and it would take 9 steps.
Then 8 steps for position 3, ... so it would take 10*10/2 steps until
it figures out the regexp does not match.
See jquery/jquery#5068
The new approach is to require "non-whitespace" char before the whitespace run,
so it spends just one step for each space in the string.1 parent 8d9efb6 commit f84b0ba
1 file changed
+4
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | | - | |
| 112 | + | |
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| |||
0 commit comments