HighDots Forums  

Trim functions

Javascript JavaScript language (comp.lang.javascript)


Discuss Trim functions in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
RobG
 
Posts: n/a

Default Trim functions - 06-02-2008 , 11:33 PM






There is an interseting examination of varions trim functions here:

<URL: http://blog.stevenlevithan.com/archi...rim-javascript >

The best all-round function seems to be trim3 when strings have lots
of leading and trailing whitespace, which is when other functions slow
down and speed is of most importance.


--
Rob


Reply With Quote
  #2  
Old   
Evertjan.
 
Posts: n/a

Default Re: Trim functions - 06-03-2008 , 02:30 AM






RobG wrote on 03 jun 2008 in comp.lang.javascript:

Quote:
There is an interseting examination of varions trim functions here:

URL: http://blog.stevenlevithan.com/archi...rim-javascript

The best all-round function seems to be trim3 when strings have lots
of leading and trailing whitespace, which is when other functions slow
down and speed is of most importance.

Interesting!

While his trim12 is very good at longer strings,
these do better at short ones:

function myTrim1 (str) {
var str = str.replace(/^\s\s*/, '');
str = str.replace(/\s\s*$/, '');
};

function trim1 (str) {
var str = str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};

His trim1 taking nearly TWICE the time myTrim1 takes in IE7 and FF2
independent of string length!

Why would that be?


======================

btw: Leaving of the ;;; does gain round 10%.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #3  
Old   
Evertjan.
 
Posts: n/a

Default Re: Trim functions - 06-03-2008 , 02:35 AM



Evertjan. wrote on 03 jun 2008 in comp.lang.javascript:

Quote:
function myTrim1 (str) {
var str = str.replace(/^\s\s*/, '');
str = str.replace(/\s\s*$/, '');
};

function trim1 (str) {
var str = str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};

His trim1 taking nearly TWICE the time myTrim1 takes in IE7 and FF2

Forget that, I mixed them up, it is the other, more logical, way around.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.