[jquery-dev] Re: Fast trim implementation

2008-11-06 Thread Ariel Flesler
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(

[jquery-dev] Re: Fast trim implementation

2008-11-06 Thread Andrea Giammarchi
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,

[jquery-dev] Re: Fast trim implementation

2008-11-06 Thread Ariel Flesler
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 >

[jquery-dev] Re: Fast trim implementation

2008-11-05 Thread Ariel Flesler
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

[jquery-dev] Re: Fast trim implementation

2008-11-05 Thread Andrea Giammarchi
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

[jquery-dev] Re: Fast trim implementation

2008-11-05 Thread John Resig
> 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

[jquery-dev] Re: Fast trim implementation

2008-11-05 Thread Andrea Giammarchi
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

[jquery-dev] Re: Fast trim implementation

2008-11-05 Thread Anthony Johnston
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

[jquery-dev] Re: Fast trim implementation

2008-11-05 Thread Andrea Giammarchi
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

[jquery-dev] Re: Fast trim implementation

2008-11-05 Thread Anthony Johnston
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

[jquery-dev] Re: Fast trim implementation

2008-11-05 Thread Andrea Giammarchi
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

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Andrea Giammarchi
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

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Andrea Giammarchi
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

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Ariel Flesler
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

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Andrea Giammarchi
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

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Andrea Giammarchi
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

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Ariel Flesler
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

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Andrea Giammarchi
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,

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Ariel Flesler
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

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Ariel Flesler
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

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Andrea Giammarchi
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

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Andrea Giammarchi
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 ); }

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Andrea Giammarchi
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

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Andrea Giammarchi
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,

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Ariel Flesler
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

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Ariel Flesler
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-dev] Re: Fast trim implementation

2008-11-04 Thread Andrea Giammarchi
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

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread Andrea Giammarchi
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){

[jquery-dev] Re: Fast trim implementation

2008-11-04 Thread chris thatcher
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