Faster JavaScript Trim Testing
Steve has write an article about faster javascript trim . He had collected many implementations of trim in javascript, and take a test to find the most efficient approach. At last, he wrote a new implementation and it is the fastest!
This article it’s great . And I found this is a very interesting subject. I was stuck on it. But Steve didn’t show up the testing codes, so I build one, with the codes here.
My Testing Page:
In this page, you’ll see three textarea at the top of page. Those are Template String list. The methods are all from faster javascript trim.
About Template String:
The string there is including the same leading whitespace and the same trailing whitespace. and these whitespace is including several kinds of characters like \s,\t,\n,\u3000.
In Firefox and opera, these whitespace are all match by \s, but not in IE. In IE, It is different from \u3000 and \s , So I replace all the \u3000 to \s in IE in order to make all the trim methods can work fine.
You can see the details of the temlate strings by making a simple test. Just double on the method or string list to start , or the start button too.
From the detail column of the result table, you’ll see something like this:
[\s28][有限状态机[...]平稳流畅。][\s61].length(898)
What does this mean? From the left to right, they are the length of leading whitespace, the trimed string, the length of trailing whitespace and the total length of the string before triming.
About the String List:
By the name of the string, you will see what the main different they are. And they are all from the template string area, so you can change them too. For example, the _shortStr and _longStr is from the first template string textarea. The different is, the _shortStr is just one time of the template string, but the _longStr 100 times of it.
Notic: the _bigWhiteSpaceStr string is real a big white leading and trailing space, and the length are 28,028 and 61,061!
Thank you.
This is a javascript practice for me,you can right click and view the page resrource any time. And I am still learning how to make codes better and better, please leave a reply if any suggestions, Thank you!
Popularity: 68% [?]
Tags: javascript, trim
文章字体大小:

Leave a Reply