The W3C team's head was kicked off until javascript1.8.1 supported the trim function.
The W3C team's head was kicked off until javascript1.8.1 supported the trim function (with trimLeft and trimRight). Unfortunately, only firefox3.5 supported it now. Since the blank spaces on both sides of the string are too common, all major types of databases have their shadows. In addition, foreigners are very engaged in research, and many implementations have been made.
Implementation 1
String.prototype.trim = function() { return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');}
It looks pretty bad. I used two regular expressions to replace it. The actual speed is amazing, mainly thanks to the internal optimization of the browser. A famous example of String concatenation is faster than the StringBuffer made of Array. The base2 class library uses this implementation.
Implementation 2
String.prototype.trim = function() { return this.replace(/^\s+/, '').replace(/\s+$/, '');}
It is similar to implementation 1, but a little slower, mainly because it first assumes that at least one blank character exists. Prototype. js uses this implementation, but its name is strip, because Prototype methods all strive to be the same name as Ruby.
Implementation 3
String.prototype.trim = function() { return this.substring(Math.max(this.search(/\S/), 0),this.search(/\S\s*$/) + 1);}
The blank part is obtained as an intercept (of course, a blank character is allowed in the middle). A total of four native methods are called. The design is very clever. substring uses two numbers as parameters. Math. max takes two numbers as parameters, and search returns a number. The speed is a little slower than the two above, but faster than most below.
Implementation 4
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, '');}
This can be called a simplified version of 2, which is to use the candidate operator to connect two regular expressions. However, in doing so, the chance of browser optimization is lost, which is inferior to 3. Because it seems elegant, many class libraries use it, such as JQuery and mootools.
Implementation 5
String.prototype.trim = function() { var str = this; str = str.match(/\S+(?:\s+\S+)*/); return str ? str[0] : '';}
Match is an array, so the elements of the original string that meet the requirements become its element. To prevent the blank characters in the string from being excluded, we need to use non-capturing grouping (? : Exp ). Since the array may be empty, we will further judge it later. It seems that the browser is weak in processing the group and the word is slow. So do not be superstitious about regular expressions, although it is basically omnipotent.
Implementation 6
String.prototype.trim = function() { return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, '$1');}
Provide the required parts and put them in an empty string. However, the efficiency is poor, especially in IE6.
Implementation 7
String.prototype.trim = function() {return this.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, '$1');}
Similar to implementation 6, but non-capturing groups are used to improve the performance.
Implementation 8
String.prototype.trim = function() {return this.replace(/^\s*((?:[\S\s]*\S)?)\s*$/, '$1');}
We improved the above two ideas and used non-capturing grouping and Character Set combination? Replaced by *, the effect is amazing. Especially in IE6, we can use crazy words to describe the performance improvement and kill Firefox in seconds.
Implementation 9
String.prototype.trim = function() {return this.replace(/^\s*([\S\s]*?)\s*$/, '$1');}
This time, we replaced the non-capturing group with lazy match, which was improved in Firefox. IE was not as crazy as it was last time.
Implementation 10
String.prototype.trim = function() { var str = this, whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000'; for (var i = 0,len = str.length; i < len; i++) { if (whitespace.indexOf(str.charAt(i)) === -1) { str = str.substring(i); break; } } for (i = str.length - 1; i >= 0; i--) { if (whitespace.indexOf(str.charAt(i)) === -1) { str = str.substring(0, i + 1); break; } } return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';}
I just want to say that the person who came up with this is not described by the ox, but is already of the same level as God. It first lists all possible blank characters, removes the leading space in the first traversal, and removes the trailing space in the second traversal. In the whole process, only the native method indexOf and substring are used to process strings, but no regular expressions are used. The speed is amazing. It is estimated that the internal binary implementation is directly pushed, and both IE and Firefox (other browsers certainly have no doubt) have a good performance. The speed is zero milliseconds.
Implementation 11
String.prototype.trim = function() { var str = this, str = str.replace(/^\s+/, ''); for (var i = str.length - 1; i >= 0; i--) { if (/\S/.test(str.charAt(i))) { str = str.substring(0, i + 1); break; } } return str;}
Implementation 10 tells us that the common native string truncation method is far better than regular expression replacement, although it is a little more complicated. However, as long as the regular expression is not complex, we can use the browser to optimize the regular expression to improve the program execution efficiency, such as 8 in IE performance. I don't think anyone will apply 10 in the project, because the whitespace implementation is too long and hard to remember (of course, if you are creating a class library, it is definitely the first one ). 11 is the ultimate version. The blank content in the previous section is cut down by regular expression replacement, and the Native method is used later. The effect is not inferior to that in the original version, but the speed is very backward.
Implementation 12
String.prototype.trim = function() { var str = this, str = str.replace(/^\s\s*/, ''), ws = /\s/, i = str.length; while (ws.test(str.charAt(--i))); return str.slice(0, i + 1);}
Implementation 10 and implementation 11 are better written in simplified versions. Note that it is not about performance speed, but easy to remember and use. And its two predecessors are both zero-millisecond-level, and they will be used to work and be scary in the future.
The following is a comparison result provided by foreigners. The execution background is to trim the article Magna Carta (more than 27,600 characters.
Implementation |
Firefox 2 |
IE 6 |
Trim1 |
15 ms |
& Lt; 0.5 ms |
Trim2 |
31 ms |
& Lt; 0.5 ms |
Trim3 |
46 ms |
31 ms |
Trim4 |
47 ms |
46 ms |
Trim5 |
156 ms |
1656 ms |
Trim6 |
172 ms |
2406 ms |
Trim7 |
172 ms |
1640 ms |
Trim8 |
281 ms |
& Lt; 0.5 ms |
Trim9 |
125 ms |
78 ms |
Trim10 |
& Lt; 0.5 ms |
& Lt; 0.5 ms |
Trim11 |
& Lt; 0.5 ms |
& Lt; 0.5 ms |
Trim12 |
& Lt; 0.5 ms |
& Lt; 0.5 ms |
This article is available at http://www.nowamagic.net/librarys/veda/detail/199.