Faster JavaScript Trim. Neat optimisation post—it turns out that while regular expressions are great for removing leading whitespace you can do a lot better at trailing whitespace by manually looping backwards from the end of the string.
Faster JavaScript Trim. Neat optimisation post—it turns out that while regular expressions are great for removing leading whitespace you can do a lot better at trailing whitespace by manually looping backwards from the end of the string.
I already poured over this article in the name of speed and, as you would probably expect, the speed comes as a function of the length of the strings you're trimming, the test string used in the article is very much larger than strings I'd usually work with (sanitising email addresses for example you'd be better using a different optimisation).
For my usecase, um, names of IM accounts buddies :-), having just one regular expression outweighed the advantage of two regular expressions.
Paul Sowden - 4th February 2008 07:40 - #
While the regex engine may not do a good job optimizing for end of string matches, I imagine that if you are sanitizing a string by matching the actual content, the greedy nature of regex would allow you to match content and any surrounding whitespace in a efficient, single expression run.
David Robarts - 5th February 2008 05:18 - #
Paul - 8th May 2008 01:00 - #