Faster JavaScript Trim
Since JavaScript doesn't include a trim method natively, it's included by countless JavaScript libraries – usually as a global function or appended to String.prototype. However, I've never seen an implementation which performs as well as it could, probably because most programmers don't deeply understand or care about regex efficiency issues.
After seeing a particularly bad trim implementation, I decided to do a little research towards finding the most efficient approach. Before getting into the analysis, here are the results:
| Method | Firefox 2 | IE 6 |
|---|---|---|
| trim1 | 15ms | < 0.5ms |
| trim2 | 31ms | < 0.5ms |
| trim3 | 46ms | 31ms |
| trim4 | 47ms | 46ms |
| trim5 | 156ms | 1656ms |
| trim6 | 172ms | 2406ms |
| trim7 | 172ms | 1640ms |
| trim8 | 281ms | < 0.5ms |
| trim9 | 125ms | 78ms |
| trim10 | < 0.5ms | < 0.5ms |
| trim11 | < 0.5ms | < 0.5ms |
Note 1: The comparison is based on trimming the Magna Carta (over 27,600 characters) with a bit of leading and trailing whitespace 20 times on my personal system. However, the data you're trimming can have a major impact on performance, which is detailed below.
Note 2: trim4 and trim6 are the most commonly found in JavaScript libraries today.
Note 3: The aforementioned bad implementation is not included in the comparison, but is shown later.
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.
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.return str.replace(/^\s+/, '').replace(/\s+$/, '');
Very similar totrim1(above), but a little slower since it doesn't trigger all of the same optimizations.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.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.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.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) totrim8. There's no good reason to use this in JavaScript, especially since it can be very slow in IE6.return str.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, '$1');
The same astrim6, 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.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.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-pass, lazy approach it uses. Liketrim8, this is significantly faster in IE6 than Firefox 2.
Since I've seen the following additional implementation in one library, I'll include it here as a warning:
return str.replace(/^\s*([\S\s]*)\b\s*$/, '$1');
Although the above is sometimes the fastest method when working with short strings which contain both non-space characters and edge whitespace, it performs very poorly with long strings which contain numerous word boundaries, and it's terrible (!) with long strings comprised of nothing but whitespace, since that triggers an exponentially increasing amount of backtracking. Do not use.
A different endgame
There are two methods in the table at the top of this post which haven't been covered yet. For those, I've used a non-regex and hybrid approach.
After comparing and analyzing all of the above, I wondered how an implementation which used no regular expressions would perform. Here's what I tried:
function trim10 (str) {
var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
for (var i = 0; i < str.length; i++) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(i);
break;
}
}
for (i = str.length - 1; i >= 0; i--) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(0, i + 1);
break;
}
}
return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}
How does that perform? Well, with long strings which do not contain excessive leading or trailing whitespace, it blows away the competition (except against trim1/2/8 in IE, which are already insanely fast there).
Does that mean regular expressions are slow in Firefox? No, not at all. The issue here is that although regexes are very well suited for trimming leading whitespace, apart from the .NET library (which offers a somewhat-mysterious "backwards matching" mode), they don't really provide a method to jump to the end of a string without even considering previous characters. However, the non-regex-reliant trim10 function does just that, with the second loop working backwards from the end of the string until it finds a non-whitespace character.
Knowing that, what if we created a hybrid implementation which combined a regex's universal efficiency at trimming leading whitespace with the alternative method's speed at removing trailing characters?
function trim11 (str) {
str = str.replace(/^\s+/, '');
for (var i = str.length - 1; i >= 0; i--) {
if (/\S/.test(str.charAt(i))) {
str = str.substring(0, i + 1);
break;
}
}
return str;
}
Although the above is a bit slower than trim10 with some strings, it uses significantly less code and is still lightning fast. Plus, with strings which contain a lot of leading whitespace (which includes strings comprised of nothing but whitespace), it's much faster than trim10.
In conclusion…
Since the differences between the implementations cross-browser and when used with different data are both complex and nuanced (none of them are faster than all the others with any data you can throw at it), here are my general recommendations for a trim method:
- Use
trim1if you want a general-purpose implementation which is fast cross-browser. - Use
trim11if you want to handle long strings exceptionally fast in all browsers.
To test all of the above implementations for yourself, try my very rudimentary benchmarking page. Background processing can cause the results to be severely skewed, so run the test a number of times (regardless of how many iterations you specify) and only consider the fastest results (since averaging the cost of background interference is not very enlightening).
As a final note, although some people like to cache regular expressions (e.g. using global variables) so they can be used repeatedly without recompilation, IMO this does not make much sense for a trim method. All of the above regexes are so simple that they typically take no more than a nanosecond to compile. Additionally, some browsers automatically cache the most recently used regexes, so a typical loop which uses trim and doesn't contain a bunch of other regexes might not encounter recompilation anyway.
Edit (2008-02-04): Shortly after posting this I realized trim10/11 could be better written. Several people have also posted improved versions in the comments. Here's what I use now, which takes the trim11-style hybrid approach:
function trim12 (str) {
var str = str.replace(/^\s\s*/, ''),
ws = /\s/,
i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
}

Comment by Shady on 8 June 2007:
Great work Steve. Grats on blowing the pants off the competition. Parsing the Magna Carta 20 times in less than a millisecond is flagrantly badass indeed.
Pingback by Pruebas de rendimiento a Trim() en javascript | aNieto2K on 9 June 2007:
[…] 11 funciones trim() puestas a prueba dan como resultado una interesante estadística que nos permite seleccionar la que más nos interese. […]
Comment by Steve on 9 June 2007:
Thanks, Shady. But really, the competition is regular expression engines themselves and how to best take advantage of them. I doubt many JavaScripters who’ve written a trim function have spent much time considering alternative approaches.
Comment by SeriousSam on 10 June 2007:
After looking at your comparison of trim methods (which in my oppinion is quite interesting) I found minor bugs in your trim10 and trim11 methods. It seems that your array indices and arguments to substr are off by 1. Consider this, for a hopefully correct trim11:
function trim( str ) {
str = str.replace(/^\s+/, ”);
for( var i = str.length-1; i > 0; –i ) {
if( /\S/.test( str[i] ) ) {
str = str.substring( 0, i+1 );
break;
}
}
return str;
}
Comment by Steve on 10 June 2007:
Fixed. Thanks.
By the way, I’d recommend avoiding treating strings as arrays to look up character indexes like that (e.g.,
str[i]). It’s not part of the ECMA-262 3rd Edition standard and doesn’t work correctly in IE. Better to stick with thecharAtmethod for accurate lookups.Pingback by All in a days work… on 12 June 2007:
[…] “Trim” the Holdup What if we created a hybrid implementation which combined a regex’s universal efficiency at trimming leading whitespace with the alternative method’s speed at removing trailing characters? (tags: RegEx JavaScript) […]
Comment by Doeke Zanstra on 6 July 2007:
Cool! Great work. This kind of research should be done more often.
I found this article, because Dean Edwards used the research for the base2 project (http://code.google.com/p/base2/). I actually did some tests on the Mac and made even another implementation, see my website.
Comment by Steve on 6 July 2007:
Thanks, Doeke! And thanks for mentioning that Dean Edwards referenced this in Base2, as I hadn’t known that previously. (By the way, through referrer logs I’ve so far discovered this post also referenced in implementations by or discussions with the developers of jQuery, Ext, Rails, and CFJS.)
For other readers, see Doeke Zanstra’s blog post for performance times of the above trim functions in several WebCore, Gecko, and Presto based browsers on Mac OS, and a better version of trim10.
Pingback by Dylan Schiemann » Blog Archive » a better way to trim on 9 July 2007:
[…] Levithan has put together by far the most extensive analysis of JavaScript and trim I have seen (thanks for the tip Dean). We’ll certainly use this knowledge in Dojo 0.9 and […]
Comment by Karl on 9 July 2007:
Just wanted to let you know Steve, that since being pointed at your blog by Dean Edwards, that its been added to the list of RSS feeds for our News Aggregator (Planet Dojo) at http://dojotoolkit.org
Great article and very interesting articles all around on your blog. Keep up the great work!
-Karl
Comment by Steve on 9 July 2007:
Karl, that’s awesome! Thanks for the heads up.
Comment by Jeff on 12 July 2007:
Interesting. My firefox actually ran trim10 the fastest. Constantly return near 0ms times. A bug perhaps, or was it just that speedy?
If I cranked it up to 60 iterations, it would show 10ms.
Regardless, very nice work.
Comment by Steve on 12 July 2007:
Jeff, it’s just that speedy. :) With the given test of trimming the Magna Carta with small amounts of whitespace at both ends, trim10 certainly should be the fastest of those shown here.
But as this blog post explains in some detail, none of these are the one-true fastest. Their comparative speeds depend largely on the data they’re fed, although some are more consistent than others cross-browser and/or with any given data.
Trim10’s strength is that, apart from edge whitespace, the length of the string has very little impact on its performance. Its weakness is strings which contain large amounts of leading or trailing whitespace, since a loop over each character won’t traverse that nearly as fast as a simple regex. That’s why I recommended trim11 over trim10, as although it’s a little slower than trim10 with many types of strings, it removes one of trim10’s weaknesses vs. the regex-based functions (long, leading whitespace).
Comment by Jeff on 20 July 2007:
True, but in all honesty, I’ve never really run into cases in the wild where the trimming that needed to be done was that out of hand.
Sure, if I make up data to be cleaned and stuff the snot outta it with spaces, it pokes a hole in the looping method — but I’ve never encountered that IRL.
So for me, I’ll happily gank trim10 :D Thanks!
Comment by Robbert Broersma on 3 November 2007:
Shortest notation:
function trim(str)
{
str = str.replace(/^\s+/, ”)
for (var i = str.length; i–;)
if (/\S/.test(str.charAt(i)))
return str.substring(0, ++i)
return str
}
Comment by Ariel Flesler on 8 November 2007:
Hi Steven, great article!
I gave it a try and wrote my own trim.. if you are interested, here’s an implementation, I tried to make it work as fast as yours.
In my PC it is sometimes a bit faster, sometimes equal.
I created a 27k long string, with 300 spaces on each side, using less than that, both were always giving 0ms….
Here’s the example: http://www.freewebs.com/flesler/Trim/
And here’s the piece of code:
var trim = (function(){ \u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';var ws = {},
chars = ' \n\r\t\v\f\u00a0\u2000\u2001\u2002\u2003\u2004\u2005
for(var i = 0; i < chars.length; i++ )
ws[chars.charAt(i)] = true;
return function( str ){
var s = -1,
e = str.length;
while( ws[str.charAt(--e)] );
while( s++ !== e && ws[str.charAt(s)] );
return str.substring( s, e+1 );
};
})();
Comment by Steve on 8 November 2007:
@Ariel: Nicely done. :-)
Comment by Ariel Flesler on 9 November 2007:
Thanks! and thank you for clearing up my mess :D
Comment by Nacho on 7 December 2007:
Hi,
Good thinking and nice research!
I was curious about already existing trim implementations and your article sums them up very nicely. Still, playing myself a little bit and being the regex lover that I am, I came up with the simpliest implementation that I could think off and I was wondering what you think about it.
String.prototype.trim = function() {
return this.replace(/^\s*(\S.*\S)?\s*$/, ‘$1′);
}
I haven’t test for speed or browser compatibility issues (I have just to support Firefox 2 & IE7 at the time being) and I’m not *very* concerned about a couple of ms difference so it could very well not be an alternative at all. Still, I’d love to hear what you have to say about it :D
Comment by Steve on 7 December 2007:
Thanks, Nacho! Re: your implementation, it is not equivalent to the others shown (in other words it’s broken). Two reasons:
- It will not work with strings which have just one non-whitespace character.
- Since JavaScript doesn’t have a “single-line” mode you need to replace the dot with
[\S\s](or similar).In any case,
/^\s+|\s+$/gis already more “simple” if you measure by readability (arguably) or number of characters.Comment by Nacho on 9 December 2007:
You completely got me there. I’m glad I posted it :)
It’s of course true, it wouldn’t validate a one non-whitespace character string. Silly that it could scape my attention.
About the “single line” mode I’m not so sure I follow you. I thought JavaScript had a flag (m) for multiline matching and therefore assumed that when the flag is not present matching only takes place on a single line strings … then again I do not know the internals as far as you do …
I guess I’ll have to give 1 or 11 a go ;)
Thanks for the comment!
Comment by Steve on 9 December 2007:
The regex terms single-line and multi-line are confusing for many people, which is why I shun them in RegexPal in favor of the more descriptive “^$ match at line breaks” (instead of “multi-line”) and “dot matches newline” (instead of “single-line”). However, “dot matches newline” mode is not available natively in JavaScript… it’s provided by XRegExp.
In other words, without XRegExp, JavaScript provides no way for the regex dot to match all characters including newlines. Multi-line mode changes the meaning of the “^” and “$” tokens — it has nothing to do with what the dot matches.
Pingback by Now Direction » JavaScript Trim Function on 12 December 2007:
[…] know my audience doesn’t care much for Web Development content, but this was a fascinating article about the various methods of trimming the white space from a text […]
Pingback by jQuery Minute™ » Performance Tuning Regular Expressions in JavaScript on 18 January 2008:
[…] Doug D started a thread on the google groups jQuery developer list about a faster trim method he ran across and how counter intuitively it was better to run two expressions rather than one. Matt Kruse then provided a link to a great article on the subject: http://blog.stevenlevithan.com/archives/faster-trim-javascript […]
Comment by Scott Trenda on 20 January 2008:
Hey Steve, excellent analysis. Thought I’d throw in my two cents here; after slogging through it, I saw Ariel already posted a near-verbatim version of the trim10 redux I’d written. :)
I did a bit of extensive testing based on your test example using variations of #10 and #11. Like you said, while #10 is nice and zippy when there’s no leading space, it’s just too slow when there’s any significant leading space. So I went with #11, and here’s what I ended up with. (Brevity first, as always! :) )
function trim13 (str)
{
var str = str.replace(/^\s*/, “”), s = /\s/, i = str.length;
while (s.test(str.charAt(–i)));
return str.substring(0, i + 1);
}
Just putting the /\S/ regex (from #11) outside of the loop sped it up for starters, about twice as fast on runs of 10000 times. A notable quirk I found with the /^\s+/ regex: it performs much worse (3400ms vs. 470ms) on strings with no leading whitespace, compared to strings with even a single leading space. Perhaps you could explain that better, but it seems /^\s*/ keeps a consistent performance in both cases.
On a different note, we seem to share the same taste in alcohol. :) Vodka + Red Bull is my mainstay at any bar, but my vodka of choice happens to be Grey Goose. And I drink entirely too much Red Bull - 4 to 6 cans on a usual workday. Keeps you running, no? ~_^
Comment by Scott Trenda on 20 January 2008:
Oh, and one more thing about the trim10 function as you have it posted now. IE doesn’t recognize ‘\v’ as a metacharacter in Javascript strings, so a literal ‘v’ ends up in the whitespace string. Try trimming a string starting with ‘v’ - it’ll chop the leading ‘v’ as well. (That one drove me nuts for a full 20 minutes.) Replace it with \x0b and all’s well. ^_^
Comment by Steve on 20 January 2008:
@Scott Trenda, interesting about IE not interpreting
\vas a vertical tab when embedded in a string literal, especially since it is handled correctly in regexes (/\v/.test("\x0b") == true). I’ll fix that intrim10.I’ve actually been using something very similar to your
trim13recently. The only (edge case) problem is that browsers interpret\sdifferently (see JavaScript, Regex, and Unicode, and the test page). I’ve left the very quick and dirtytrim10/11up there since their ugliness seems to have inspired others to improve them.As for your observations about
^\s*vs.^\s+, that depends on the implementation. Another alternative to consider is^\s\s*. The difference stems from internal optimizations like whether or not a pre-check of required characters is performed, and the relative cost of success vs. failure to match.Pingback by Ajaxian » JavaScript Trim Optimizations on 3 February 2008:
[…] Simon found this gem. Steven Levithan wrote about optimizing a JavaScript trim. […]
Comment by Mark on 3 February 2008:
The method that GWT uses to translate String.trim():
public native String trim() /*-{
var r1 = this.replace(/^(\s*)/, ”);
var r2 = r1.replace(/\s*$/, ”);
return r2;
}-*/;
Comment by Alexey on 4 February 2008:
Is really actual on “MS Windows” and “Internet Explorer”? you cannot fix bugs by javascript.
Comment by Daniel Steigerwald on 4 February 2008:
Hi,
nice work. But one thing I’m missing is point to different /s handling in browsers.
ECMAScript specifies \s as [\t\n\v\f\r], Firefox added [\u00A0\u2028\u2029] to the list.
Opera happens to match with \s like Firefox. Safari behaves like the IE and doesnt match   with \s.
more: http://dev.mootools.net/ticket/646
I didnt test \u2028 and \u2029, but I think /[\s\u00A0\u2028\u2029]+/g should fix this for all browsers, as it adds firefox`s additions to \s.
Comment by Steve on 4 February 2008:
@Daniel Steigerwald, that’s not entirely correct. See my post on JavaScript, regex, and Unicode for more information about what
\sshould and does match.Pingback by Javascript News » JavaScript Trim Optimizations on 4 February 2008:
[…] Simon found this gem. Steven Levithan wrote about optimizing a JavaScript trim. […]
Comment by Tiziano on 4 February 2008:
ERROR in function 10 and 11:
for (var i = str.length - 1; i > 0; i–) {
=> for (var i = str.length - 1; i >= 0; i–) {
Comment by Steve on 4 February 2008:
@Tiziano, that is not an error. The way they are written, there is no reason for the backwards loops to be concerned about the character at index 0.
Comment by Aristotle Pagaltzis on 4 February 2008:
It seems to me that your loop does far more work than necessary. Why shorten the string one character at a time? Keep the loop counter around and you can do all the shortening at once. That’s bound to be faster.
var i = str.length - 1;while ( i >= 0 && /\s/.test(str.charAt(i)) ) --i;
str = str.substring(0, i + 1);
Also, it might be worth pulling the regex construction out of the loop; that depends on how well Javascript compilers optimise.
var ws = /\s/;while ( i >= 0 && ws.test(str.charAt(i)) ) --i;
Comment by Aristotle Pagaltzis on 4 February 2008:
True, but anyone who ever modifies the code needs to be aware that there’s an inactive bug in the code, lest they accidentally activate it. And it doesn’t cost anything at all to make the check correct. So there’s no reason not to fix it.
Comment by Steve on 4 February 2008:
@Aristotle Pagaltzis, putting the regex outside the loop shouldn’t matter according to ECMA-262 3rd Edition since the spec states that regex literals cause only one object to be created at runtime for a script or function. However, most implementations don’t respect that (Firefox does), and in any case the behavior is proposed to be changed in ECMAScript 4.
Regarding changing the loop counter to allow an extra iteration… I’ve realized that Tiziano was correct. It was in fact an error in the case where there is whitespace to the right and only one non-whitespace character. I’ve fixed it, but the trim10/11 implementations are ugly anyway, as you’ve pointed out. Although I’ve intentionally been avoiding this for some time, I’ve gone ahead and edited the post to show a cleaner version of the
trim11approach at the end (which is nearly identical to what Scott Trenda posted earlier).Pingback by A faster JavaScript Trim | foojam.com on 4 February 2008:
[…] is an older article, but Steven Levithan has an article posted on his site regarding how to do a faster trim function in JavaScript. His demo page has eleven different trim implementations and some example text to test them out […]
Comment by Yves on 4 February 2008:
Did anybody tried str.lastIndexOf() to find the trailing blanks ?
Pingback by SiNi Daily » JavaScript Trim Optimizations on 4 February 2008:
[…] JavaScript Trim Optimizations February 4, 2008 – 2:32 pm | by SiNi Simon found this gem. Steven Levithan wrote about optimizing a JavaScript trim. […]
Comment by Aristotle Pagaltzis on 4 February 2008:
@Yves:
You can’t use
lastIndexOffor this problem. That method only gives you a way to ask for the last appearance of a specific character, but what we need is a way to ask for the last appearance of any other character than a space. Additionally,\sin a regex doesn’t find just space characters, but a number of other whitespace characters as well. You can’t do that withlastIndexOfat all.@Steve:
Now that I think of it, how does the following version fare?
return str.replace(/^\s+/, '').replace(/.*\s+$/, '');This should be faster than any of the regex approaches you showed above. A class like
[\s\S]is kinda silly – “match anything that’s whitespace or is not whitespace” is just a long-winded way to say “match anything,” except that non-IE browsers should be able to optimise it as well, and in fact many regex engines have special optimisations for.*built in. This should gobble up the entire string immediately and then do the same backtrack-loop as the explicit Javascript code does, except without crossing back and forth between the JS VM and the RE engine at every backtracking step in order to involve the JS VM dispatcher.But that’s just theory-based hypothesis – benchmarking is in order to confirm (or disprove) it.
Comment by Aristotle Pagaltzis on 4 February 2008:
Oh! D’uh. Disregard the above suggestion. That won’t work for obvious reasons.
I got confused because I do this in conjunction with
\zsin Vim all the time. In Vim you could writes/.*\zs\s\s*$//and it would replace just the part after the\zs. In Perl 5.10 you can do the same using the\Kescape. But Javascript has neither extension, so… yeah.Comment by Steve on 4 February 2008:
\Kwould be very nice to have, especially since JavaScript has no lookbehind. But yeah, you can’t do that. Note that something like[\S\s]is necessary because JavaScript has no “single-line” (dot matches all) mode.Comment by Aristotle Pagaltzis on 4 February 2008:
OK, I think I’ve unbrainfarted myself enough to actually try my idea of using a greedy match and RE engine backtracking. Sorry for all the noise. Here’s
trim12:function trim12 (str) {
var str = str.replace(/^\s\s*/, ''),
len = str.length;
if (len && /\s/.test(str.charAt(len-1)) {
var re = /.*\S/g;
re.test(str);
str = str.slice(0, re.lastIndex);
}
return str;
}
The trick here is as follows. The inner regex is run only if the string is non-empty, which means there must be non-whitespace characters in it, because otherwise the first substitution would have left an empty string, and only if the last character is whitespace. In that case, we run a global match that first gobbles up the entire string using
.*, then backtracks until it can match\S. We know it must match because at this point we know the string ends with whitespace and we know it has non-whitespace characters in it. After the match, because it is global (/gflag), the position of the character after the end of the match will be recorded in thelastIndexproperty of the regex object.So we just use that to return the portion of the string before it.
Please benchmark this. I’ve tested it and I know it works; now the question is how fast it is.
Comment by Aristotle Pagaltzis on 4 February 2008:
Argh, now I see that. I guess explicitness would demand
[-\uFFFF], but that’s clearly more cumbersome to type and read than[\s\S]. Sigh.(Hopefully I will stop spamming your comments now. Sorry again.)
Comment by jag on 4 February 2008:
Is there any performance gain from using /\s*\s$/ instead of /\s+$/ or /\s*$/?
Pingback by links for 2008-02-05 « Simply… A User on 4 February 2008:
[…] Faster JavaScript Trim Since JavaScript doesn’t include a trim method natively, it’s included by countless JavaScript libraries – usually as a global function or appended to String.prototype. (tags: javascript performance trim regex string optimization regexp tips **) […]
Pingback by afongen » links for 2008-02-05 on 5 February 2008:
[…] Faster JavaScript Trim (tags: javascript regex) […]
Pingback by Trim in Javascript / Melodycode.com - Life is a flash on 5 February 2008:
[…] per la visita!Steven Levithan ha sentito la necessità (ebbravo!) di studiare come sia possibile ottimizzare la funzione trim che di solito viene inclusa come libreria esterna in Javascript. La conclusione è la seguente (vi […]
Comment by Haoest on 5 February 2008:
Funny, IE beats Firefox most of the rounds. If only they have spent as much effort on adhering to the standard…
Pingback by benstraw.com » links for 2008-02-05 on 5 February 2008:
[…] http://blog.stevenlevithan.com/archives/faster-trim-javascript (tags: benchmark development javascript optimization trim regex string) […]
Comment by Scott Blum on 5 February 2008:
Steve, I have a couple of questions about the 2008-02-04 update:
1) What happens in the empty string case? Seems like “i” is -1 and bad stuff should happen?
2) What’s the licensing around this? I’d like to use it in GWT (we’re Apache 2.0).
Thanks,
Scott
Comment by Steve on 5 February 2008:
@Scott Blum:
1. An
ivalue of-1works fine becausecharAtreturns an empty string if the provided index is out of range.2. Any code I post on my blog is under the MIT license unless otherwise specified. But something this simple I’d consider public domain. You’re certainly very welcome to use it in GWT.
Comment by Joonas Lehtolahti on 6 February 2008:
Hello, nice article. Using the benchmark page with Opera 9.5 beta snapshot build 9755 on Windows, I got some interesting results.
With 20 iterations the methods 1-8 were between 150 and 450 ms, trim9 took almost 4 seconds! trim10 and trim12 were 0 ms every time, but trim11 was at 15 ms.
With 100 iteration the methods 1-8 were quite similar as with 20 iterations, but the values five times larger naturally. Similarly trim9 was almost 19 seconds (slow!). Still trim10 gave a nice result of 0 ms, while both trim11 and trim12 were 47 ms.
With 200 iterations I finally managed to make trim10 take more than 0 ms of time, it took 15 ms in one run, which itself took almost a minute to run, thanks to trim9’s slow 39 seconds of execution time! Here, however, trim12 was slower than trim11 for some reason, while with 20 iterations it was always a lot faster.
So, at least with Opera 9.5 it seems like the method 10 is clearly superior to all of the other methods.
Comment by Steve on 6 February 2008:
@Joonas Lehtolahti, that may be true with the provided test data, but this post makes it clear that none of the methods are fastest with all types of strings.
Trim9 is slow in Opera because that browser is particularly bad at lazy quantification.
Pingback by Fatih Hayrioğlu’nun not defteri » 11 Şubat Web’den seçme haberler on 11 February 2008:
[…] Javascript ile metin kırma işlemleri ve hız faktörü hakkında bir makale. Bağlantı […]
Comment by Chris Akers on 21 February 2008:
It seems to me that the right trim of #1 is a little out of order. The current #1 trim shows:
str.replace(/^\s\s*/, ”).replace(/\s\s*$/, ”);
Shouldn’t it be changed to:
str.replace(/^\s\s*/, ”).replace(/\s*\s$/, ”);
It is symmetric with the left trim and it would seem that a pre-check optimization could potentially be more easily performed by using only the last character. [I admit that I might be over-thinking this…]
Comment by Steve on 22 February 2008:
@Chris Akers, you are assuming that such a pre-check would account for position relative to particular anchors within the string. That is unlikely. But more importantly, you would potentially be making the second regex take longer to fail at positions prior to string-ending whitespace. That tiny bit of extra time (assuming the absence of certain types of other potential internal optimizations) would add up over the course of checking every position in a long string.
Pingback by News » Faster JavaScript Trim on 23 February 2008:
[…] Faster JavaScript Trim. Neat optimisation post—it turns out that while regular expressions are great for removing leading whitespace you can do a lot better at trailing whitespace by manually looping backwards from the end of the string. […]
Comment by Sean on 24 February 2008:
What about something like this?
String.prototype.trim = function(){
var s = /\s*([\S+\s*]*\S+)+\s*/i.exec(this);
return (!s) ? ” : s[1];
}
Pingback by Well Read & Misinformed» String.prototype Fun! on 24 February 2008:
[…] note: I found this nifty site that compares a variety of String.trim methods ( I wonder how mine compares ), it’s worth reading : Faster JavaScript Trim […]
Comment by Steve on 26 February 2008:
@Sean, I think you fundamentally misunderstand how your regular expression works. For starters, you should read up on regex character class syntax. With all whitespace, your regular expression manages to be orders of magnitude worse performing than every other regular expression on this page, including the one that was so bad I had to leave it out of the speed tests.