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 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){
> return function(String){return String.replace(RegExp, "")}
> }(/^\s+|\s+$/g);
>
> 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
>
> $ = {};
> $.trim = function(RegExp){
> return function(String){return String.replace(RegExp, "")}
> }(/^\s+|\s+$/g);
>
> var trim = (function(){
> var ws = {},
> chars = '
> \n\r\t\v\f\u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
> 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 );
> };
> })();
>
> for(var i = 0, s = "", t = new Date; i < 100000; i++)
> trim(s);
> alert(new Date - t); // 501
>
>
> for(var i = 0, s = "", t = new Date; i < 100000; i++)
> $.trim(s);
> alert(new Date - t); // 284
>
> Maybe we need more exhaustive tests?
>
> Regards
>
>
> On Tue, Nov 4, 2008 at 5:00 PM, chris thatcher <
> [EMAIL PROTECTED]> wrote:
>
>> 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 Flesler <[EMAIL PROTECTED]>wrote:
>>
>>>
>>>
>>> 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
>>>
>>>
>>
>>
>> --
>> Christopher Thatcher
>>
>>
>> >>
>>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---