Trim functions

E

Evertjan.

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/archives/faster-trim-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%.
 
E

Evertjan.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top