The W3C team's head was kicked off until javascript1.8.1 supported the trim function (with trimleft and trimright). Unfortunately, only firefox3.5 supported it now. Since the blank spaces on both sides of the string are too common, all major types of databases have their shadows. In addition, foreigners are very engaged in research, and many implementations have been made.
Implementation 1
1.
String. Prototype. Trim =
Function
(){
2.
Return
This
. Replace (/^ \ s */,
''
). Replace (/\ s * $ /,
''
);
3.
}
It looks pretty bad. I used two regular expressions to replace it. The actual speed is amazing, mainly thanks to the internal optimization of the browser. A famous example of String concatenation is faster than the stringbuffer made of array. The base2 class library uses this implementation.
Implementation 2
1.
String. Prototype. Trim =
Function
(){
2.
Return
This
. Replace (/^ \ s + /,
''
). Replace (/\ s + $ /,
''
);
3.
}
It is similar to implementation 1, but a little slower, mainly because it first assumes that at least one blank character exists. Prototype. js uses this implementation, but its name is strip, because prototype methods all strive to be the same name as Ruby.
Implementation 3
1.
String. Prototype. Trim =
Function
(){
2.
Return
This
. Substring (math. Max (
This
. Search (/\ s/), 0 ),
This
. Search (/\ s * $/) + 1 );
3.
}
The blank part is obtained as an intercept (of course, a blank character is allowed in the middle). A total of four native methods are called. The design is very clever. substring uses two numbers as parameters. Math. Max takes two numbers as parameters, and search returns a number. The speed is a little slower than the two above, but faster than most below.
Implementation 4
1.
String. Prototype. Trim =
Function
(){
2.
Return
This
. Replace (/^ \ s + | \ s + $/g,
''
);
3.
}
This can be called a simplified version of 2, which is to use the candidate operator to connect two regular expressions. However, in doing so, the chance of browser optimization is lost, which is inferior to 3. Because it seems elegant, many class libraries use it, such as jquery and mootools.
Implementation 5
1.
String. Prototype. Trim =
Function
(){
2.
VaR
STR =
This
;
3.
STR = Str. Match (/\ s + (? : \ S + )*/);
4.
Return
Str? STR [0]:
''
;
5.
}
Match is an array, so the elements of the original string that meet the requirements become its element. To prevent the blank characters in the string from being excluded, we need to use non-capturing grouping (? : Exp ). Since the array may be empty, we will further judge it later. It seems that the browser is weak in processing the group and the word is slow. So do not be superstitious about regular expressions, although it is basically omnipotent.
Implementation 6
1.
String. Prototype. Trim =
Function
(){
2.
Return
This
. Replace (/^ \ s * (\ s +) *) \ s * $ /,
'$1'
);
3.
}
Provide the required parts and put them in an empty string. However, the efficiency is poor, especially in IE6.
Implementation 7
1.
String. Prototype. Trim =
Function
(){
2.
Return
This
. Replace (/^ \ s *(? : \ S +) *) \ s * $ /,
'$1'
);
3.
}
Similar to implementation 6, but non-capturing groups are used to improve the performance.
Implementation 8
1.
String. Prototype. Trim =
Function
(){
2.
Return
This
. Replace (/^ \ s *((? : [\ S] * \ s )?) \ S * $ /,
'$1'
);
3.
}
We improved the above two ideas and used non-capturing grouping and Character Set combination? Replaced by *, the effect is amazing. Especially in IE6, we can use crazy words to describe the performance improvement and kill Firefox in seconds.
Implementation 9
1.
String. Prototype. Trim =
Function
(){
2.
Return
This
. Replace (/^ \ s * ([\ s] *?) \ S * $ /,
'$1'
);
3.
}
This time, we replaced the non-capturing group with lazy match, which was improved in Firefox. ie was not as crazy as it was last time.
Implementation 10
01.
String. Prototype. Trim =
Function
(){
02.
VaR
STR =
This
,
03.
Whitespace =
'\ N \ r \ t \ f \ x0b \ xa0 \ u2000 \ u2001 \ u2002 \ u2003 \ u2004 \ u2005 \ U2006 \ U2007 \ u2008 \ u2009 \ u200a \ u200b \ u2028 \ u2029 \ u3000'
;
04.
For
(
VaR
I = 0, Len = Str. length; I <Len; I ++ ){
05.
If
(Whitespace. indexof (Str. charat (I) ===-1 ){
06.
STR = Str. substring (I );
07.
Break
;
08.
}
09.
}
10.
For
(I = Str. Length-1; I> = 0; I --){
11.
If
(Whitespace. indexof (Str. charat (I) ===-1 ){
12.
STR = Str. substring (0, I + 1 );
13.
Break
;
14.
}
15.
}
16.
Return
Whitespace. indexof (Str. charat (0) ===-1? STR:
''
;
17.
}
I just want to say that the person who came up with this is not described by the ox, but is already of the same level as God. It first lists all possible blank characters, removes the leading space in the first traversal, and removes the trailing space in the second traversal. In the whole process, only the native method indexof and substring are used to process strings, but no regular expressions are used. The speed is amazing. It is estimated that the internal binary implementation is directly pushed, and both IE and Firefox (other browsers certainly have no doubt) have a good performance. The speed is zero milliseconds.
Implementation 11
01.
String. Prototype. Trim =
Function
(){
02.
VaR
STR =
This
,
03.
STR = Str. Replace (/^ \ s + /,
''
);
04.
For
(
VaR
I = Str. Length-1; I> = 0; I --){
05.
If
(/\ S/. Test (Str. charat (I ))){
06.
STR = Str. substring (0, I + 1 );
07.
Break
;
08.
}
09.
}
10.
Return
STR;
11.
}
Implementation 10 tells us that the common native string truncation method is far better than regular expression replacement, although it is a little more complicated. However, as long as the regular expression is not complex, we can use the browser to optimize the regular expression to improveProgramExecution efficiency, such as the performance of 8 in IE. I don't think anyone will apply 10 in the project, because the whitespace implementation is too long and hard to remember (of course, if you are creating a class library, it is definitely the first one ). 11 is the ultimate version. The blank content in the previous section is cut down by regular expression replacement, and the Native method is used later. The effect is not inferior to that in the original version, but the speed is very backward.
Implementation 12
1.
String. Prototype. Trim =
Function
(){
2.
VaR
STR =
This
,
3.
STR = Str. Replace (/^ \ s */,
''
),
4.
Ws =/\ s /,
5.
I = Str. length;
6.
While
(WS. Test (Str. charat (-- I )));
7.
Return
Str. Slice (0, I + 1 );
8.
}
Implementation 10 and implementation 11 are better written in simplified versions. Note that it is not about performance speed, but easy to remember and use. And its two predecessors are both zero-millisecond-level, and they will be used to work and be scary in the future.
The following is a comparison result provided by foreigners. The execution background is for Magna CartaArticleTrim (more than 27,600 characters.
Implementation |
Firefox 2 |
IE 6 |
trim1 |
15 Ms |
<0.5 ms |
trim2 |
31 Ms |
<0.5 ms |
trim3 |
46 Ms |
31 Ms |
trim4 |
47 Ms |
46 Ms |
trim5 |
156 Ms |
1656 Ms |
trim6 |
172 Ms |
2406 Ms |
trim7 |
172 Ms |
1640 Ms |
trim8 |
281 Ms |
<0.5 ms |
trim9 |
125 Ms |
78 Ms |
trim10 |
<0.5 ms |
<0.5 ms |
trim11 |
<0.5 ms |
<0.5 ms |
trim12 |
<0.5 ms |
<0.5 ms |
Link: http://blog.stevenlevithan.com/archives/faster-trim-javascript
Note that this article is not translated. I just published my own ideas based on the trim function implementation mentioned in this Article. If you want to know what the original author says, please read the original article.