Improving String.strip performance

0 views
Skip to first unread message

disccomp

unread,
May 14, 2008, 1:05:33 PM5/14/08
to Prototype: Core
Please see this excellent breakdown of this function on cross-browser
performance to better understand the available solutions.
http://blog.stevenlevithan.com/archives/faster-trim-javascript

John-David Dalton

unread,
May 14, 2008, 2:01:03 PM5/14/08
to Prototype: Core
Also some other performance tips:
http://www.sitepen.com/blog/2008/05/09/string-performance-an-analysis/

disccomp, feel free to submit a ticket and a patch for the current
String#strip
http://www.prototypejs.org/contribute

- JDD

Ken Snyder

unread,
May 14, 2008, 2:44:28 PM5/14/08
to prototy...@googlegroups.com
Awesome analysis!

I wonder if trim12 might be improved by avoiding calling slice if there
is no trailing white space:

function trim12 (str) {
var str = str.replace(/^\s\s*/, ''),
ws = /\s/,
i = str.length,
len = i;
while (ws.test(str.charAt(--i)));
return i + 1 < len ? str.slice(0, i + 1) : str;
}


Also, you did not say it explicitly, but are you saying that /\s/.test()
is faster than whitespace.indexOf() ?

- Ken

disccomp

unread,
May 21, 2008, 9:58:08 AM5/21/08
to Prototype: Core
I'd prefer if someone already involved in the project would use the
analysis to create the ticket, patch, etc..
Reply all
Reply to author
Forward
0 new messages