Move this topic
Fast trim implementation
in Developing jQuery Core
•
3 years ago
http://groups.google.com/group/jquery-en/browse_thread/thread/09abbc3bc6e14cdd
Thoughts on this ? can you find any flaw ?
Should it get to the core ?
--
Ariel Flesler
http://flesler.blogspot.com
Replies(29)
Re: Fast trim implementation
3 years ago
Nice, I've been using Steves trim12 for awhile, I guess I hadn't seen your earlier blog. Is there are significant difference in the time for trimming a large number of small strings versus a single large string? Just curious, but good work.
+1
Thatcher
+1
Thatcher
Leave a comment on thatcher.christopher's reply
Re: Fast trim implementation
3 years ago
It's really weird that a manual implementation of a regexp, that should be executed in core and should basically perform a really similar task you are performing manually, is faster ...
I wonder if we really need an "error prone" code instead of something like this:
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">$.trim = function(RegExp){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return function(String){return String.replace(RegExp, "")}</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">}(/^\s+|\s+$/g);</span>
avoiding the creation of a new regexp each time and considering new JS engines "they" are offering to us.
Above function, as example, performs about 1.5 times faster than your proposal in my FireFox 3
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">$ = {};</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">$.trim = function(RegExp){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return function(String){return String.replace(RegExp, "")}</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">}(/^\s+|\s+$/g);</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">var trim = (function(){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var ws = {},</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> chars = ' \n\r\t\v\f\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> for(var i = 0; i < chars.length; i++ )</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ws[chars.charAt(i)] = true;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return function( str ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var s = -1,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> e = str.length;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> while( ws[str.charAt(--e)] );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while( s++ !== e && ws[str.charAt(s)] );</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return str.substring( s, e+1 );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> };</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">})();</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">for(var i = 0, s = "", t = new Date; i < 100000; i++)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> trim(s);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">alert(new Date - t); // 501</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">for(var i = 0, s = "", t = new Date; i < 100000; i++)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> $.trim(s);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">alert(new Date - t); // 284</span>
Maybe we need more exhaustive tests?
Regards
I wonder if we really need an "error prone" code instead of something like this:
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">$.trim = function(RegExp){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return function(String){return String.replace(RegExp, "")}</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">}(/^\s+|\s+$/g);</span>
avoiding the creation of a new regexp each time and considering new JS engines "they" are offering to us.
Above function, as example, performs about 1.5 times faster than your proposal in my FireFox 3
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">$ = {};</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">$.trim = function(RegExp){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return function(String){return String.replace(RegExp, "")}</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">}(/^\s+|\s+$/g);</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">var trim = (function(){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var ws = {},</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> chars = ' \n\r\t\v\f\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> for(var i = 0; i < chars.length; i++ )</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ws[chars.charAt(i)] = true;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return function( str ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var s = -1,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> e = str.length;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> while( ws[str.charAt(--e)] );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while( s++ !== e && ws[str.charAt(s)] );</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return str.substring( s, e+1 );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> };</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">})();</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">for(var i = 0, s = "", t = new Date; i < 100000; i++)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> trim(s);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">alert(new Date - t); // 501</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">for(var i = 0, s = "", t = new Date; i < 100000; i++)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> $.trim(s);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">alert(new Date - t); // 284</span>
Maybe we need more exhaustive tests?
Regards
Leave a comment on andrea.giammarchi's reply
Re: Fast trim implementation
3 years ago
jQuery core ready implementation:
<span style="font-family: courier new,monospace;">trim: function( re ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return function( text ){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return ( text || "" ).replace( re, "" )</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }( /^\s+|\s+$/g ),</span>
:-)
<span style="font-family: courier new,monospace;">trim: function( re ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return function( text ){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return ( text || "" ).replace( re, "" )</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }( /^\s+|\s+$/g ),</span>
:-)
Leave a comment on andrea.giammarchi's reply
Re: Fast trim implementation
3 years ago
2 things:
- Use the one called myBestTrim... that one, as the name indicates, is
the best version.
- A regex-based trim can do well for small strings (even more for
empty ones), but scales horribly.
They actually scale so badly that I had to remove the test called
'jQueryTrim' from the "large string suite". It took like x2-4 longer
than the rest.
I doubt the engines do create a new regexp each time they reach it.
It's a clear constant that they can cache.
IMO it's important to keep it balanced between short and long strings.
jQuery's actual trim (similar to yours) causes problems when doing
stuff like:
$( some_long_ahah_response )....
Cheers
P.S: I knew you'd be throwin' in your super revolutionary version of
it.. so predictable :-P
Leave a comment on flesler's reply
Re: Fast trim implementation
3 years ago
There're 2 suites, one with a large string (10K chars) and one with a
small(30 chars) strings.
Both seem to yield positive results for my last function. I think it
gets better as the length increases, but still outperforms on small
ones.
Leave a comment on flesler's reply
Leave a comment on andrea.giammarchi's reply
Re: Fast trim implementation
3 years ago
<span style="font-family: verdana,sans-serif;">Ariel,</span><br style="font-family: verdana,sans-serif;"><span style="font-family: verdana,sans-serif;">I read now the myBestTrim function.</span><br style="font-family: verdana,sans-serif;">
<br style="font-family: verdana,sans-serif;"><span style="font-family: verdana,sans-serif;">You check charCodeAt less than 33, but in the precedent version you used these chars:</span><br style="font-family: verdana,sans-serif;">
<code style="font-size: 90%; font-family: verdana,sans-serif;">chars = ' \n\r\t\v\f\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
I wonder which side effect could we have ignoring thos u2XXX characters, that as far as I know, are not included in range 0, 33 ... am I wrong?
</code>
<br style="font-family: verdana,sans-serif;"><span style="font-family: verdana,sans-serif;">You check charCodeAt less than 33, but in the precedent version you used these chars:</span><br style="font-family: verdana,sans-serif;">
<code style="font-size: 90%; font-family: verdana,sans-serif;">chars = ' \n\r\t\v\f\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
I wonder which side effect could we have ignoring thos u2XXX characters, that as far as I know, are not included in range 0, 33 ... am I wrong?
</code>
Leave a comment on andrea.giammarchi's reply
Re: Fast trim implementation
3 years ago
<span style="font-family: verdana,sans-serif;">P.S. there is a logic problem, and probably a performances improvement using ++start instead of start++</span><br style="font-family: verdana,sans-serif;"><br style="font-family: verdana,sans-serif;">
<pre class="js" style="letter-spacing: -0.05em; font-family: verdana,sans-serif;"><span style="font-family: courier new,monospace;" class="keyword">function</span><span style="font-family: courier new,monospace;"> myBestTrim( str ){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;" class="tab"> </span><span style="font-family: courier new,monospace;" class="keyword">var</span><span style="font-family: courier new,monospace;"> start = </span><span style="font-family: courier new,monospace;" class="number">-1</span><span style="font-family: courier new,monospace;">,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;" class="tab"> </span><span style="font-family: courier new,monospace;">end = str.</span><span style="font-family: courier new,monospace;" class="method">length</span><span style="font-family: courier new,monospace;">;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;" class="tab"> </span><span style="font-family: courier new,monospace;" class="keyword">while</span><span style="font-family: courier new,monospace;">(str.</span><span style="font-family: courier new,monospace;" class="method">charCodeAt</span><span style="font-family: courier new,monospace;">(--end) < </span><span style="font-family: courier new,monospace;" class="number">33</span><span style="font-family: courier new,monospace;">);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;" class="tab"> </span><span style="font-family: courier new,monospace;" class="keyword">while</span><span style="font-family: courier new,monospace;">(</span><b style="font-family: courier new,monospace;">++start</b><span style="font-family: courier new,monospace;"> < end && str.</span><span style="font-family: courier new,monospace;" class="method">charCodeAt</span><span style="font-family: courier new,monospace;">(start) < </span><span style="font-family: courier new,monospace;" class="number">33</span><span style="font-family: courier new,monospace;">);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;" class="tab"> </span><span style="font-family: courier new,monospace;" class="keyword">return</span><span style="font-family: courier new,monospace;"> str.</span><span style="font-family: courier new,monospace;" class="method">slice</span><span style="font-family: courier new,monospace;">( start, end + </span><span style="font-family: courier new,monospace;" class="number">1</span><span style="font-family: courier new,monospace;"> );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">};</span>
</pre><span style="font-family: verdana,sans-serif;">In your code start++ is obviously less than end since value === 0 for the first case will be true only on right side, the charCodeAt</span><br style="font-family: verdana,sans-serif;">
I know it was just a silly error and just one more boolean evaluation that does not make that difference, but why do not fix it ;-)
<pre class="js" style="letter-spacing: -0.05em; font-family: verdana,sans-serif;"><span style="font-family: courier new,monospace;" class="keyword">function</span><span style="font-family: courier new,monospace;"> myBestTrim( str ){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;" class="tab"> </span><span style="font-family: courier new,monospace;" class="keyword">var</span><span style="font-family: courier new,monospace;"> start = </span><span style="font-family: courier new,monospace;" class="number">-1</span><span style="font-family: courier new,monospace;">,</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;" class="tab"> </span><span style="font-family: courier new,monospace;">end = str.</span><span style="font-family: courier new,monospace;" class="method">length</span><span style="font-family: courier new,monospace;">;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;" class="tab"> </span><span style="font-family: courier new,monospace;" class="keyword">while</span><span style="font-family: courier new,monospace;">(str.</span><span style="font-family: courier new,monospace;" class="method">charCodeAt</span><span style="font-family: courier new,monospace;">(--end) < </span><span style="font-family: courier new,monospace;" class="number">33</span><span style="font-family: courier new,monospace;">);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;" class="tab"> </span><span style="font-family: courier new,monospace;" class="keyword">while</span><span style="font-family: courier new,monospace;">(</span><b style="font-family: courier new,monospace;">++start</b><span style="font-family: courier new,monospace;"> < end && str.</span><span style="font-family: courier new,monospace;" class="method">charCodeAt</span><span style="font-family: courier new,monospace;">(start) < </span><span style="font-family: courier new,monospace;" class="number">33</span><span style="font-family: courier new,monospace;">);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;" class="tab"> </span><span style="font-family: courier new,monospace;" class="keyword">return</span><span style="font-family: courier new,monospace;"> str.</span><span style="font-family: courier new,monospace;" class="method">slice</span><span style="font-family: courier new,monospace;">( start, end + </span><span style="font-family: courier new,monospace;" class="number">1</span><span style="font-family: courier new,monospace;"> );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">};</span>
</pre><span style="font-family: verdana,sans-serif;">In your code start++ is obviously less than end since value === 0 for the first case will be true only on right side, the charCodeAt</span><br style="font-family: verdana,sans-serif;">
I know it was just a silly error and just one more boolean evaluation that does not make that difference, but why do not fix it ;-)
Leave a comment on andrea.giammarchi's reply
Re: Fast trim implementation
3 years ago
P.S.2 ... in php for example ++$var is a bit faster than $var++ , dunno in JS which one performs better, that was the other improvement that is true in my old laptop
Leave a comment on andrea.giammarchi's reply
Re: Fast trim implementation
3 years ago
Leave a comment on flesler's reply
Re: Fast trim implementation
3 years ago
Yeah, ++foo should be faster++ though the results were exactly the
same when I tested for like 2K iterations. Still, can be changed.
It remain from the former version, where start++ !== end was used (
!== instead of < ). It was then safe to use that operator only with
the pluses on right.
!== should be faster than <, but again, the results were the same, so
I just chose the shorter one.
Leave a comment on flesler's reply
Re: Fast trim implementation
3 years ago
Last suggestion.
The charCodeAt(--end) returns NaN when end is negative, and NaN < 33 is false.
We can use this trick on start variable as well, removing one check for each character.
As summary, I wonder which performances could have this version:
<span style="font-family: courier new,monospace;">function myBestTrim( str ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var start = -1, end;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if(end = str.length){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> while(str.charCodeAt( --end ) < 33);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while(str.charCodeAt( ++start ) < 33);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> str = str.slice( start, end + 1 );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> };</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return str;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">};</span>
Which seems to work fine for me.
Regards.
The charCodeAt(--end) returns NaN when end is negative, and NaN < 33 is false.
We can use this trick on start variable as well, removing one check for each character.
As summary, I wonder which performances could have this version:
<span style="font-family: courier new,monospace;">function myBestTrim( str ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var start = -1, end;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if(end = str.length){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> while(str.charCodeAt( --end ) < 33);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while(str.charCodeAt( ++start ) < 33);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> str = str.slice( start, end + 1 );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> };</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return str;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">};</span>
Which seems to work fine for me.
Regards.
Leave a comment on andrea.giammarchi's reply
Re: Fast trim implementation
3 years ago
That does, but will iterate doubly for strings made of spaces. That's
why I added the check.
But yeah, could do. I'll benchmark the different options asap (not today).
Leave a comment on flesler's reply
Re: Fast trim implementation
3 years ago
to save more space, compact version entirely based on NaN trick:
<span style="font-family: courier new,monospace;">function myBestTrim( str ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var start = -1, end = str.length;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while(str.charCodeAt( --end ) < 33);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> while(str.charCodeAt( ++start ) < 33);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return str.slice( start, end + 1 );</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">};</span>
Thoughts?
<span style="font-family: courier new,monospace;">function myBestTrim( str ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var start = -1, end = str.length;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while(str.charCodeAt( --end ) < 33);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> while(str.charCodeAt( ++start ) < 33);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return str.slice( start, end + 1 );</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">};</span>
Thoughts?
Leave a comment on andrea.giammarchi's reply
Re: Fast trim implementation
3 years ago
But in this case you are checking twice for each character, start less than end plus the other check.
For small strings the best option culd be return the regexp version /^\s+|\s+$/g while for strings with length more than N (dunno which length is big enough) we can use my last proposal without problems, a charcode instead of start < end wont make the difference, IMO :-)
For small strings the best option culd be return the regexp version /^\s+|\s+$/g while for strings with length more than N (dunno which length is big enough) we can use my last proposal without problems, a charcode instead of start < end wont make the difference, IMO :-)
Leave a comment on andrea.giammarchi's reply
Leave a comment on flesler's reply
Re: Fast trim implementation
3 years ago
something like ...
<span style="font-family: courier new,monospace;">function myBestTrim( str ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var start = -1, end = str.length;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if(end < 1000)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return str.replace(/^\s+|\s+$/g, "");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while(str.charCodeAt( --end ) < 33);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> while(str.charCodeAt( ++start ) < 33);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return str.slice( start, end + 1 );</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">};</span>
<span style="font-family: courier new,monospace;">function myBestTrim( str ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var start = -1, end = str.length;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if(end < 1000)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return str.replace(/^\s+|\s+$/g, "");</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while(str.charCodeAt( --end ) < 33);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> while(str.charCodeAt( ++start ) < 33);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return str.slice( start, end + 1 );</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">};</span>
Leave a comment on andrea.giammarchi's reply
Leave a comment on andrea.giammarchi's reply
Re: Fast trim implementation
3 years ago
I did just a couple of tests, and this seems to be the most compact and fast in any case.
<span style="font-family: courier new,monospace;">function myBestTrim( str ){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> var start = -1, end = str.length;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while(str.charCodeAt( --end ) < 33);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> while(str.charCodeAt( ++start ) < 33);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return str.slice( start, end + 1 );</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">};</span>
<span style="font-family: courier new,monospace;">function myBestTrim( str ){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> var start = -1, end = str.length;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while(str.charCodeAt( --end ) < 33);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> while(str.charCodeAt( ++start ) < 33);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return str.slice( start, end + 1 );</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">};</span>
Leave a comment on andrea.giammarchi's reply
Re: Fast trim implementation
3 years ago
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Arial","sans-serif";
color:windowtext;
font-weight:normal;
font-style:normal;
text-decoration:none none;}
.MsoChpDefault
{mso-style-type:export-only;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=EN-GB link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Could
you split this so that we have trimstart and trimend?<o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p> </o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Ant<o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p> </o:p></span>
<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'>
<p class=MsoNormal><b><span lang=EN-US style='font-size:10.0pt;font-family:
"Tahoma","sans-serif"'>From:</span></b><span lang=EN-US style='font-size:10.0pt;
font-family:"Tahoma","sans-serif"'> jquery-dev@googlegroups.com
[mailto:jquery-dev@googlegroups.com] <b>On Behalf Of </b>Andrea Giammarchi
<b>Sent:</b> 05 November 2008 09:03
<b>To:</b> jquery-dev@googlegroups.com
<b>Subject:</b> [jquery-dev] Re: Fast trim implementation<o:p></o:p></span>
</div>
<p class=MsoNormal><o:p> </o:p>
<p class=MsoNormal style='margin-bottom:12.0pt'>I did just a couple of tests,
and this seems to be the most compact and fast in any case.
<span style='font-family:"Courier New"'>function myBestTrim( str ){
var start = -1, end = str.length;
while(str.charCodeAt( --end ) < 33);
while(str.charCodeAt( ++start ) < 33);
return str.slice( start, end + 1 );
};</span><o:p></o:p>
<div>
<p class=MsoNormal>There is probably one case where it does not perform faster,
length 1 or 0, but we are talking abut 1 to 5 miliseconds in both cases, and
about "0" with new browsers possibilities.
2 proposals:
<span style='font-family:"Courier New"'>
trim: function( text ) {
var str = text || "", start =
-1, end = str.length;
while(str.charCodeAt( --end ) < 33);
while(str.charCodeAt( ++start ) < 33);
return str.slice( start, end + 1 );
},
trim: function( text ) {
if(text){
var start = -1, end =
text.length;
while(
text.charCodeAt( --end ) < 33 );
while(
text.charCodeAt( ++start ) < 33 );
return text.slice(
start, end + 1 );
};
return "";
},</span>
Regards
On Tue, Nov 4, 2008 at 10:14 PM, Andrea Giammarchi <<a
href="mailto:andrea.giammarchi@gmail.com">andrea.giammarchi@gmail.com</a>>
wrote:<o:p></o:p>
<p class=MsoNormal>Cheers :-)<o:p></o:p>
<div>
<div>
<p class=MsoNormal style='margin-bottom:12.0pt'><o:p> </o:p>
<div>
<p class=MsoNormal>On Tue, Nov 4, 2008 at 10:11 PM, Ariel Flesler <<a
href="mailto:aflesler@gmail.com" target="_blank">aflesler@gmail.com</a>>
wrote:<o:p></o:p>
<p class=MsoNormal>
Will benchmark all this.
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Arial","sans-serif";
color:windowtext;
font-weight:normal;
font-style:normal;
text-decoration:none none;}
.MsoChpDefault
{mso-style-type:export-only;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=EN-GB link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Could
you split this so that we have trimstart and trimend?<o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p> </o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Ant<o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p> </o:p></span>
<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'>
<p class=MsoNormal><b><span lang=EN-US style='font-size:10.0pt;font-family:
"Tahoma","sans-serif"'>From:</span></b><span lang=EN-US style='font-size:10.0pt;
font-family:"Tahoma","sans-serif"'> jquery-dev@googlegroups.com
[mailto:jquery-dev@googlegroups.com] <b>On Behalf Of </b>Andrea Giammarchi
<b>Sent:</b> 05 November 2008 09:03
<b>To:</b> jquery-dev@googlegroups.com
<b>Subject:</b> [jquery-dev] Re: Fast trim implementation<o:p></o:p></span>
</div>
<p class=MsoNormal><o:p> </o:p>
<p class=MsoNormal style='margin-bottom:12.0pt'>I did just a couple of tests,
and this seems to be the most compact and fast in any case.
<span style='font-family:"Courier New"'>function myBestTrim( str ){
var start = -1, end = str.length;
while(str.charCodeAt( --end ) < 33);
while(str.charCodeAt( ++start ) < 33);
return str.slice( start, end + 1 );
};</span><o:p></o:p>
<div>
<p class=MsoNormal>There is probably one case where it does not perform faster,
length 1 or 0, but we are talking abut 1 to 5 miliseconds in both cases, and
about "0" with new browsers possibilities.
2 proposals:
<span style='font-family:"Courier New"'>
trim: function( text ) {
var str = text || "", start =
-1, end = str.length;
while(str.charCodeAt( --end ) < 33);
while(str.charCodeAt( ++start ) < 33);
return str.slice( start, end + 1 );
},
trim: function( text ) {
if(text){
var start = -1, end =
text.length;
while(
text.charCodeAt( --end ) < 33 );
while(
text.charCodeAt( ++start ) < 33 );
return text.slice(
start, end + 1 );
};
return "";
},</span>
Regards
On Tue, Nov 4, 2008 at 10:14 PM, Andrea Giammarchi <<a
href="mailto:andrea.giammarchi@gmail.com">andrea.giammarchi@gmail.com</a>>
wrote:<o:p></o:p>
<p class=MsoNormal>Cheers :-)<o:p></o:p>
<div>
<div>
<p class=MsoNormal style='margin-bottom:12.0pt'><o:p> </o:p>
<div>
<p class=MsoNormal>On Tue, Nov 4, 2008 at 10:11 PM, Ariel Flesler <<a
href="mailto:aflesler@gmail.com" target="_blank">aflesler@gmail.com</a>>
wrote:<o:p></o:p>
<p class=MsoNormal>
Will benchmark all this.
Leave a comment on guest's reply
Re: Fast trim implementation
3 years ago
I suppose the most used, generaly speacking, is trim.
As you said, trim is the result of left and right trim but since what people need is trim speed, I do not think is a good idea to create a trim function that return trimstart(trimend(text)) so the code has to be a little bit redundant, but trim performances will be preserved (avoiding two function calls for each trim)
As sum, what do you think about this simple plugin?
<span style="font-family: courier new,monospace;">;jQuery.extend({</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> trimstart:function( text ){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if( text ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var start = -1;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while( text.charCodeAt( ++start ) < 33 );</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return text.substring( start );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> };</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return "";</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> },</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> trimend:function( text ){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if( text ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var end = text.length;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while( text.charCodeAt( --end ) < 33 );</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return text.substring( 0, end + 1 );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> };</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return "";</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">});</span>
As you said, trim is the result of left and right trim but since what people need is trim speed, I do not think is a good idea to create a trim function that return trimstart(trimend(text)) so the code has to be a little bit redundant, but trim performances will be preserved (avoiding two function calls for each trim)
As sum, what do you think about this simple plugin?
<span style="font-family: courier new,monospace;">;jQuery.extend({</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> trimstart:function( text ){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if( text ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var start = -1;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while( text.charCodeAt( ++start ) < 33 );</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return text.substring( start );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> };</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return "";</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> },</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> trimend:function( text ){</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if( text ){</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> var end = text.length;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> while( text.charCodeAt( --end ) < 33 );</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return text.substring( 0, end + 1 );</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> };</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return "";</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">});</span>
Leave a comment on andrea.giammarchi's reply
Re: Fast trim implementation
3 years ago
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p
{mso-style-priority:99;
mso-margin-top-alt:auto;
margin-right:0cm;
mso-margin-bottom-alt:auto;
margin-left:0cm;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Arial","sans-serif";
color:windowtext;
font-weight:normal;
font-style:normal;
text-decoration:none none;}
.MsoChpDefault
{mso-style-type:export-only;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=EN-GB link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Fair
enough, <o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>I
had submitted a plugin for trimming and other text based functions a while back<o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><a
href="http://plugins.jquery.com/project/text">http://plugins.jquery.com/project/text</a><o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p> </o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>it’s
similar to what you have come up with here, but not so tight – also I
think I over cooked it a bit </span><span style='font-size:10.0pt;font-family:
Wingdings'>J</span><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>I’ll
revise it with your ideas. If that’s ok?<o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p> </o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Also
on a sort of other topic, is there a general consensus that jQuery does not add
prototype functions to standard objects such as String, where trimming would
ideally live.<o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p> </o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Ant<o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p> </o:p></span>
<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'>
<p class=MsoNormal><b><span lang=EN-US style='font-size:10.0pt;font-family:
"Tahoma","sans-serif"'>From:</span></b><span lang=EN-US style='font-size:10.0pt;
font-family:"Tahoma","sans-serif"'> jquery-dev@googlegroups.com
[mailto:jquery-dev@googlegroups.com] <b>On Behalf Of </b>Andrea Giammarchi
<b>Sent:</b> 05 November 2008 11:22
<b>To:</b> jquery-dev@googlegroups.com
<b>Subject:</b> [jquery-dev] Re: Fast trim implementation<o:p></o:p></span>
</div>
<p class=MsoNormal><o:p> </o:p>
<p class=MsoNormal style='margin-bottom:12.0pt'>I suppose the most used,
generaly speacking, is trim.
As you said, trim is the result of left and right trim but since what people
need is trim speed, I do not think is a good idea to create a trim function
that return trimstart(trimend(text)) so the code has to be a little bit
redundant, but trim performances will be preserved (avoiding two function calls
for each trim)
As sum, what do you think about this simple plugin?
<span style='font-family:"Courier New"'>;jQuery.extend({
trimstart:function( text ){
if( text ){
var start = -1;
while(
text.charCodeAt( ++start ) < 33 );
return text.substring(
start );
};
return "";
},
trimend:function( text ){
if( text ){
var end = text.length;
while(
text.charCodeAt( --end ) < 33 );
return text.substring(
0, end + 1 );
};
return "";
}
});</span>
<o:p></o:p>
<div>
<p class=MsoNormal>On Wed, Nov 5, 2008 at 9:33 AM, Anthony Johnston <<a
href="mailto:anthony.johnston@antix.co.uk">anthony.johnston@antix.co.uk</a>>
wrote:<o:p></o:p>
<div>
<div>
<div style='border:none;border-top:solid windowtext 1.0pt;padding:3.0pt 0cm 0cm 0cm;
border-color:-moz-use-text-color -moz-use-text-color'>
</div>
<div>
<div>
<p style='margin-bottom:12.0pt'>I did just a couple of tests, and this seems to
be the most compact and fast in any case.
function myBestTrim( str ){
var start = -1, end = str.length;
while(str.charCodeAt( --end ) < 33);
while(str.charCodeAt( ++start ) < 33);
return str.slice( start, end + 1 );
};<o:p></o:p>
<div>
<div>
<div>
<p style='margin-bottom:12.0pt'> <o:p></o:p>
<div>
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p
{mso-style-priority:99;
mso-margin-top-alt:auto;
margin-right:0cm;
mso-margin-bottom-alt:auto;
margin-left:0cm;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Arial","sans-serif";
color:windowtext;
font-weight:normal;
font-style:normal;
text-decoration:none none;}
.MsoChpDefault
{mso-style-type:export-only;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=EN-GB link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Fair
enough, <o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>I
had submitted a plugin for trimming and other text based functions a while back<o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><a
href="http://plugins.jquery.com/project/text">http://plugins.jquery.com/project/text</a><o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p> </o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>it’s
similar to what you have come up with here, but not so tight – also I
think I over cooked it a bit </span><span style='font-size:10.0pt;font-family:
Wingdings'>J</span><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>I’ll
revise it with your ideas. If that’s ok?<o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p> </o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Also
on a sort of other topic, is there a general consensus that jQuery does not add
prototype functions to standard objects such as String, where trimming would
ideally live.<o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p> </o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Ant<o:p></o:p></span>
<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p> </o:p></span>
<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm'>
<p class=MsoNormal><b><span lang=EN-US style='font-size:10.0pt;font-family:
"Tahoma","sans-serif"'>From:</span></b><span lang=EN-US style='font-size:10.0pt;
font-family:"Tahoma","sans-serif"'> jquery-dev@googlegroups.com
[mailto:jquery-dev@googlegroups.com] <b>On Behalf Of </b>Andrea Giammarchi
<b>Sent:</b> 05 November 2008 11:22
<b>To:</b> jquery-dev@googlegroups.com
<b>Subject:</b> [jquery-dev] Re: Fast trim implementation<o:p></o:p></span>
</div>
<p class=MsoNormal><o:p> </o:p>
<p class=MsoNormal style='margin-bottom:12.0pt'>I suppose the most used,
generaly speacking, is trim.
As you said, trim is the result of left and right trim but since what people
need is trim speed, I do not think is a good idea to create a trim function
that return trimstart(trimend(text)) so the code has to be a little bit
redundant, but trim performances will be preserved (avoiding two function calls
for each trim)
As sum, what do you think about this simple plugin?
<span style='font-family:"Courier New"'>;jQuery.extend({
trimstart:function( text ){
if( text ){
var start = -1;
while(
text.charCodeAt( ++start ) < 33 );
return text.substring(
start );
};
return "";
},
trimend:function( text ){
if( text ){
var end = text.length;
while(
text.charCodeAt( --end ) < 33 );
return text.substring(
0, end + 1 );
};
return "";
}
});</span>
<o:p></o:p>
<div>
<p class=MsoNormal>On Wed, Nov 5, 2008 at 9:33 AM, Anthony Johnston <<a
href="mailto:anthony.johnston@antix.co.uk">anthony.johnston@antix.co.uk</a>>
wrote:<o:p></o:p>
<div>
<div>
<span style='font-size:10.0pt'>Could you split this so that we have
trimstart and trimend?</span><o:p></o:p>
<span style='font-size:10.0pt'> </span><o:p></o:p>
<span style='font-size:10.0pt'>Ant</span><o:p></o:p>
<span style='font-size:10.0pt'> </span><o:p></o:p>
<div style='border:none;border-top:solid windowtext 1.0pt;padding:3.0pt 0cm 0cm 0cm;
border-color:-moz-use-text-color -moz-use-text-color'>
<b><span lang=EN-US style='font-size:10.0pt'>From:</span></b><span
lang=EN-US style='font-size:10.0pt'> <a
href="mailto:jquery-dev@googlegroups.com" target="_blank">jquery-dev@googlegroups.com</a>
[mailto:<a href="mailto:jquery-dev@googlegroups.com" target="_blank">jquery-dev@googlegroups.com</a>]
<b>On Behalf Of </b>Andrea Giammarchi
<b>Sent:</b> 05 November 2008 09:03
<b>To:</b> <a href="mailto:jquery-dev@googlegroups.com" target="_blank">jquery-dev@googlegroups.com</a>
<b>Subject:</b> [jquery-dev] Re: Fast trim implementation</span><o:p></o:p>
</div>
<div>
<div>
<o:p></o:p>
<p style='margin-bottom:12.0pt'>I did just a couple of tests, and this seems to
be the most compact and fast in any case.
function myBestTrim( str ){
var start = -1, end = str.length;
while(str.charCodeAt( --end ) < 33);
while(str.charCodeAt( ++start ) < 33);
return str.slice( start, end + 1 );
};<o:p></o:p>
<div>
There is probably one case where it does not perform faster, length 1 or 0,
but we are talking abut 1 to 5 miliseconds in both cases, and about
"0" with new browsers possibilities.
2 proposals:
trim: function( text ) {
var str = text || "", start =
-1, end = str.length;
while(str.charCodeAt( --end ) < 33);
while(str.charCodeAt( ++start ) < 33);
return str.slice( start, end + 1 );
},
trim: function( text ) {
if(text){
var start = -1, end =
text.length;
while(
text.charCodeAt( --end ) < 33 );
while(
text.charCodeAt( ++start ) < 33 );
return text.slice(
start, end + 1 );
};
return "";
},
Regards
On Tue, Nov 4, 2008 at 10:14 PM, Andrea Giammarchi <<a
href="mailto:andrea.giammarchi@gmail.com" target="_blank">andrea.giammarchi@gmail.com</a>>
wrote:<o:p></o:p>
Cheers :-)<o:p></o:p>
<div>
<div>
<p style='margin-bottom:12.0pt'> <o:p></o:p>
<div>
On Tue, Nov 4, 2008 at 10:11 PM, Ariel Flesler <<a
href="mailto:aflesler@gmail.com" target="_blank">aflesler@gmail.com</a>>
wrote:<o:p></o:p>
Will benchmark all this.
Leave a comment on guest's reply
Re: Fast trim implementation
3 years ago
Anthony for me it is fine but maybe you should ask to Ariel since the original idea and tricky code is his stuff :-)
Leave a comment on andrea.giammarchi's reply
Re: Fast trim implementation
3 years ago
> There is probably one case where it does not perform faster, length 1 or 0,
Leave a comment on jeresig's reply
Re: Fast trim implementation
3 years ago
John I agree, that's why I did not included the check in my two proposals. In my opinion the compact one (#1) is good enough, the other one is just more "paranoic" and designed for a pessimistic usage of trim ... I mean passing empty strings should be a rare case, isn't it?
Leave a comment on andrea.giammarchi's reply
Re: Fast trim implementation
3 years ago
Ok, I'll benchmark #1 (hopefully today). I'll then create a ticket and
commit the fastest/shortest trim.
Leave a comment on flesler's reply
Re: Fast trim implementation
3 years ago
As expected, took a little less w/o that check, but x2 when all spaces.
I think that's not a situation to worry about.
Thanks Andrea for noticing.
Leave a comment on flesler's reply
Re: Fast trim implementation
3 years ago
Your welcome Ariel, in any case in your blog put a ++ before start because that -- does not sound that good :D
<pre class="js" style="letter-spacing: -0.05em;"><span class="comment">// Licensed under BSD</span>
<span class="keyword">function</span> myBestTrim( str ){
<span class="tab"> </span><span class="keyword">var</span> start = <span class="number">-1</span>,
<span class="tab"> </span><span class="tab"> </span>end = str.<span class="method">length</span>;
<span class="tab"> </span><span class="keyword">while</span>( str.<span class="method">charCodeAt</span>(--end) < <span class="number">33</span> );
<span class="tab"> </span><span class="keyword">while</span>( str.<span class="method">charCodeAt</span>(--start) < <span class="number">33</span> ); // ++start is better, isn't it? :-)
<span class="tab"> </span><span class="keyword">return</span> str.<span class="method">slice</span>( start, end + <span class="number">1</span> );
};
</pre>
Kind Regards
<pre class="js" style="letter-spacing: -0.05em;"><span class="comment">// Licensed under BSD</span>
<span class="keyword">function</span> myBestTrim( str ){
<span class="tab"> </span><span class="keyword">var</span> start = <span class="number">-1</span>,
<span class="tab"> </span><span class="tab"> </span>end = str.<span class="method">length</span>;
<span class="tab"> </span><span class="keyword">while</span>( str.<span class="method">charCodeAt</span>(--end) < <span class="number">33</span> );
<span class="tab"> </span><span class="keyword">while</span>( str.<span class="method">charCodeAt</span>(--start) < <span class="number">33</span> ); // ++start is better, isn't it? :-)
<span class="tab"> </span><span class="keyword">return</span> str.<span class="method">slice</span>( start, end + <span class="number">1</span> );
};
</pre>
Kind Regards
Leave a comment on andrea.giammarchi's reply
Leave a comment on flesler's reply
Change topic type
Link this topic
Provide the permalink of a topic that is related to this topic
Reply to flesler's discussion
{"z-1":[14737000000461285,14737000000461289],"z2566198":[14737000000461293],"z1493374":[14737000000461249,14737000000461251,14737000000461257,14737000000461259,14737000000461261,14737000000461263,14737000000461269,14737000000461273,14737000000461275,14737000000461279,14737000000461281,14737000000461283,14737000000461287,14737000000461291,14737000000461295,14737000000461301],"z2656670":[14737000000461245,14737000000461253,14737000000461255,14737000000461265,14737000000461267,14737000000461271,14737000000461277,14737000000461297,14737000000461299,14737000000461303],"z2949163":[14737000000461247]}
Statistics
- 29 Replies
- 971 Views
- 0 Followers



