473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Trim functions

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

Jun 27 '08 #1
2 1162
RobG wrote on 03 jun 2008 in comp.lang.javascript:
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)
Jun 27 '08 #2
Evertjan. wrote on 03 jun 2008 in comp.lang.javascript:
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)
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous'...
7
by: Sascha Herpers | last post by:
Hi, what is the difference between the trim function and the trim String-member? As far as I see it, both return the trimmed string and leave the original string unaltered. Is any of the two...
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
3
by: Shailesh Humbad | last post by:
I compiled this article on trim functions for Javascript: Javascript Trim LTrim and RTrim Functions http://www.somacon.com/p355.php If you have any comments, please let me know. Thanks,...
6
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I think in built-in C/C++ functions, there is no direct function call to trim heading and trailing space characters, right? So, we have to implement it manually by iterating...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.