日期:2014-05-16  浏览次数:20484 次

js_trim()十二种实现

The analysis
Although there are 11 rows in the table above, they are only the most notable (for various reasons) of about 20 versions I wrote and benchmarked against various types of strings. The following analysis is based on testing in Firefox 2.0.0.4, although I have noted where there are major differences in IE6.

1.
-----------------------------
//return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');

function trim(str) {
??? return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');

}
-------------------------------
All things considered, this is probably the best all-around approach. Its speed advantage is most notable with long strings — when efficiency matters. The speed is largely due to a number of optimizations internal to JavaScript regex interpreters which the two discrete regexes here trigger. Specifically, the pre-check of required character and start of string anchor optimizations, possibly among others.

2.
-------------------------------
return str.replace(/^\s+/, '').replace(/\s+$/, '');
-------------------------------
Very similar to trim1 (above), but a little slower since it doesn't trigger all of the same optimizations.

3.
-------------------------------
return str.substring(Math.max(str.search(/\S/), 0), str.search(/\S\s*$/) + 1);
-------------------------------
This is often faster than the following methods, but slower than the above two. Its speed comes from its use of simple, character-index lookups.

4.
-------------------------------
return str.replace(/^\s+|\s+$/g, '');
-------------------------------
This commonly thought up approach is easily the most frequently used in JavaScript libraries today. It is generally the fastest implementation of the bunch only when working with short strings which don't include leading or trailing whitespace. This minor advantage is due in part to the initial-character discrimination optimization it triggers. While this is a relatively decent performer, it's slower than the three methods above when working with longer strings, because the top-level alternation prevents a number of optimizations which could otherwise kick in.

5.
-------------------------------
str = str.match(/\S+(?:\s+\S+)*/);
return str ? str[0] : '';
-------------------------------
This is generally the fastest method when working with empty or whitespace-only strings, due to the pre-check of required character optimization it triggers. Note: In IE6, this can be quite slow when working with longer strings.

6.
-------------------------------
return str.replace(/^\s*(\S*(\s+\S+)*)\s*$/, '$1');
-------------------------------
This is a relatively common approach, popularized in part by some leading JavaScripters. It's similar in approach (but inferior) to trim8. There's no good reason to use this in JavaScript, especially since it can be very slow in IE6.

7.
-------------------------------
return str.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, '$1');
-------------------------------
The same as trim6, but a bit faster due to the use of a non-capturing group (which doesn't work in IE 5.0 and lower). Again, this can be slow in IE6.


8.
-------------------------------
return str.replace(/^\s*((?:[\S\s]*\S)?)\s*$/, '$1');
-------------------------------
This uses a simple, single-pass, greedy approach. In IE6, this is crazy fast! The performance difference indicates that IE has superior optimization for quantification of "any character" tokens.

9.
-------------------------------
return str.replace(/^\s*([\S\s]*?)\s*$/, '$1');
-------------------------------
This is generally the fastest with very short strings which contain both non-space characters and edge whitespace. This minor advantage is due to the simple, single-pas