- Notifications
You must be signed in to change notification settings - Fork20.6k
Commit3c82ebb
committed
Deprecated: improve .trim for strings containing lots of whitespaces
The old implementation took O(N^2) time to trim the string when multipleadjacent 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 realizesthe 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 untilit figures out the regexp does not match.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 parent0f6c3d9 commit3c82ebb
1 file changed
+17
-4
lines changedLines changed: 17 additions & 4 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
15 | 15 |
| |
16 | 16 |
| |
17 | 17 |
| |
18 |
| - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
19 | 25 |
| |
20 | 26 |
| |
21 | 27 |
| |
| |||
79 | 85 |
| |
80 | 86 |
| |
81 | 87 |
| |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
82 | 92 |
| |
83 |
| - | |
84 |
| - | |
85 |
| - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
86 | 99 |
| |
87 | 100 |
|
0 commit comments
Comments
(0)