I’ve recently discovered the very interesting Steven Levithan post about the JavaScript’s trim function of the String class.
So, here my version:
function trim13 (str) {
var ws = /\s/, _start = 0, end = str.length;
while(ws.test(str.charAt(_start++)));
while(ws.test(str.charAt(--end)));
return str.slice(_start - 1, end + 1);
}
More numbers, please!
I tested my function against the benchmarking page of the original post. Note: times are expressed in MS and are the average of ten executions per browser. Update [2008-05-29]: I re-runned all the tests and updated results, because Steven noticed that test suite was wrong (few whitespaces).