Heh, it's so confusing to edit html in a textarea... :P
On Thu, Nov 6, 2008 at 11:18 AM, Andrea Giammarchi
<[EMAIL PROTECTED]> wrote:
> Your welcome Ariel, in any case in your blog put a ++ before start because
> that -- does not sound that good :D
>
> // Licensed under BSD
> function myBestTrim(
Your welcome Ariel, in any case in your blog put a ++ before start because
that -- does not sound that good :D
// Licensed under BSD
function myBestTrim( str ){
var start = -1,
end = str.length;
while( str.charCodeAt(--end) < 33 );
while( str.charCodeAt(--start) < 33 ); // ++start is better,
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.
On Wed, Nov 5, 2008 at 11:21 AM, Ariel Flesler <[EMAIL PROTECTED]> wrote:
> Ok, I'll benchmark #1 (hopefully today). I'll then create a ticket and
>
Ok, I'll benchmark #1 (hopefully today). I'll then create a ticket and
commit the fastest/shortest trim.
On Wed, Nov 5, 2008 at 10:13 AM, John Resig <[EMAIL PROTECTED]> wrote:
>
>> There is probably one case where it does not perform faster, length 1 or 0,
>> but we are talking abut 1 to 5 milise
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?
On Wed, Nov 5, 2008 at
> 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.
I don't think this should really be optimized - everything is going to
be fast for incredibly small values lik
f Of *Andrea Giammarchi
> *Sent:* 05 November 2008 11:22
>
> *To:* jquery-dev@googlegroups.com
> *Subject:* [jquery-dev] Re: Fast trim implementation
>
>
>
> I suppose the most used, generaly speacking, is trim.
> As you said, trim is the result of left and right trim but
nt: 05 November 2008 11:22
To: jquery-dev@googlegroups.com
Subject: [jquery-dev] Re: Fast trim implementation
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 t
Giammarchi
> *Sent:* 05 November 2008 09:03
> *To:* jquery-dev@googlegroups.com
> *Subject:* [jquery-dev] Re: Fast trim implementation
>
>
>
> I did just a couple of tests, and this seems to be the most compact and
> fast in any case.
>
> function myBestTrim( str ){
&g
Could you split this so that we have trimstart and trimend?
Ant
From: jquery-dev@googlegroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Andrea Giammarchi
Sent: 05 November 2008 09:03
To: jquery-dev@googlegroups.com
Subject: [jquery-dev] Re: Fast trim implementation
I did just a couple
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 );
};
There is proba
Cheers :-)
On Tue, Nov 4, 2008 at 10:11 PM, Ariel Flesler <[EMAIL PROTECTED]> wrote:
>
> Will benchmark all this.
>
> On Tue, Nov 4, 2008 at 7:10 PM, Andrea Giammarchi
> <[EMAIL PROTECTED]> wrote:
> > But in this case you are checking twice for each character, start less
> than
> > end plus the o
something like ...
function myBestTrim( str ){
var start = -1, end = str.length;
if(end < 1000)
return str.replace(/^\s+|\s+$/g, "");
while(str.charCodeAt( --end ) < 33);
while(str.charCodeAt( ++start ) < 33);
return str.slice( start, end + 1 );
};
On Tue, Nov 4, 2008
Will benchmark all this.
On Tue, Nov 4, 2008 at 7:10 PM, Andrea Giammarchi
<[EMAIL PROTECTED]> wrote:
> 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 whil
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 pro
to save more space, compact version entirely based on NaN trick:
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 );
};
Thoughts?
On Tue, Nov 4, 2008 at 10:00
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).
On Tue, Nov 4, 2008 at 7:00 PM, Andrea Giammarchi
<[EMAIL PROTECTED]> wrote:
> Last suggestion.
>
> The charCodeAt(--end) return
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:
function myBestTrim( str ){
var start = -1,
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.
!== sho
On Tue, Nov 4, 2008 at 6:34 PM, Andrea Giammarchi
<[EMAIL PROTECTED]> wrote:
> Ariel,
> I read now the myBestTrim function.
>
> You check charCodeAt less than 33, but in the precedent version you used
> these chars:
> chars = '
> \n\r\t\v\f\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u20
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
On Tue, Nov 4, 2008 at 9:42 PM, Andrea Giammarchi <
[EMAIL PROTECTED]> wrote:
> P.S. there is a logic problem, and probably a perfo
P.S. there is a logic problem, and probably a performances improvement using
++start instead of start++
function myBestTrim( str ){
var start = -1,
end = str.length;
while(str.charCodeAt(--end) < 33);
while(*++start* < end && str.charCodeAt(start) < 33);
return str.slice( start, end + 1 );
}
Ariel,
I read now the myBestTrim function.
You check charCodeAt less than 33, but in the precedent version you used
these chars:
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
On Tue, Nov 4, 2008 at 5:58 PM, Ariel Flesler <[EMAIL PROTECTED]> wrote:
>
> P.S: I knew you'd be throwin' in your super revolutionary version of
> it.. so predictable :-P
:D
mine was just a suggestion. Clever or recent browsers cast as constant the
regexp, but to be sure it happens everywhere,
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.
On Tue, Nov 4, 2008 at 2:00 PM, chris thatcher
<[EMAIL PROT
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 st
jQuery core ready implementation:
trim: function( re ){
return function( text ){
return ( text || "" ).replace( re, "" )
}
}( /^\s+|\s+$/g ),
:-)
On Tue, Nov 4, 2008 at 5:35 PM, Andrea Giammarchi <
[EMAIL PROTECTED]> wrote:
> It's really weird that a manual imple
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:
$.trim = function(RegExp){
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
On Tue, Nov 4, 2008 at 11:44 AM, Ariel Fl
29 matches
Mail list logo