In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the implementation methods of JavaScript's trim function?" interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what are the trim function implementation methods of JavaScript?"
Implement 1String.prototype.trim = function () {return this.replace (/ ^\ s\ s). Replace (/\ s\ s)
It doesn't look good. Two regular replacements are used, and the actual speed is amazing, mainly due to the internal optimization of the browser. A famous example of string concatenation, direct addition is faster than StringBuffer made of Array. The base2 class library uses this implementation.
Implement 2String.prototype.trim = function () {return this.replace (/ ^\ sharmless Universe,'') .replace (/\ sharmless raceme,'');}
It is similar to implementation 1, but a little slower, mainly because it first assumes that there is at least one blank character. Prototype.js uses this implementation, but its name is strip, because Prototype's methods strive to have the same name as Ruby.
Implement 3String.prototype.trim = function () {return this.substring (Math.max (this.search (/\ S /), 0), this.search (/\ S\ skeeper /) + 1);}
The white space is intercepted (of course, white space is allowed in the middle), and a total of four native methods are called. It is so cleverly designed that substring takes two numbers as parameters. Math.max takes two numbers as parameters, and search returns one number. It's a little slower than the top two, but faster than most of the bottom ones.
Implement 4String.prototype.trim = function () {return this.replace (/ ^\ s + |\ ssteps UniUniverse g,'');}
This can be called a simplified version of implementation 2, which uses candidate operators to connect two regularities. But doing so loses the opportunity for browser optimization, which is not as good as implementing 3. Because it looks elegant, many class libraries use it, such as JQuery and mootools
Implement 5String.prototype.trim = function () {var str = this;str = str.match (/\ S+ (?:\ s +\ S+) * /); return str? Str [0]:';}
Match returns an array, so the part of the original string that meets the requirements becomes its element. To prevent the white space in the middle of the string from being excluded, we need to use the non-capture grouping (?: exp). Since the array may be empty, we will make a further decision later. It seems that the browser is relatively weak in dealing with grouping, and one word is slow. So don't blindly believe in regularity, even though it is basically omnipotent.
Implement 6String.prototype.trim = function () {return this.replace (/ ^\ s * (\ S* (\ s +\ S+) *),'$1');}
Provide the parts that meet the requirements and put them in an empty string. But it is inefficient, especially in IE6.
Implement 7String.prototype.trim = function () {return this.replace (/ ^\ s * (\ S* (?:\ s +\ S+) *),'$1');}
It is very similar to implementation 6, but with the advantage of non-capture grouping, the performance is slightly improved.
Implement 8String.prototype.trim = function () {return this.replace (/ ^\ s * ((?: [\ S\ s] *\ S)?)\ s,'$1');}
Along the above two ideas for improvement, the use of non-capture groups and character sets, with? Replaced *, the effect is very amazing. Especially in IE6, you can use madness to describe this performance improvement, directly killing Firefox.
Implement 9String.prototype.trim = function () {return this.replace (/ ^\ s * ([\ S\ s] *?),'$1');}
This time the lazy match is used to replace the non-capture grouping, which has been improved in Firefox, and IE is not as crazy as it was last time.
Implement 10String.prototype.trim = function () {var str = this,whitespace ='\ n\ r\ t\ f\ x0b\ xa0\ u2000\ u2001\ u2002\ u2004\ u2005\ u2006\ u2007\ u2008\ u2009\ u200a\ u200b\ u2028\ u2029\ u3000transactions for (var I = 0Len = str.length; I
< len; i++) {if (whitespace.indexOf(str.charAt(i)) === -1) {str = str.substring(i);break;}}for (i = str.length - 1; i >= 0; str.charAt -) {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 created this is no longer described as a cow, but at a divine level. First, it lists all the possible white space characters, cutting off the front white space in the first traversal and the following white space the second time. The whole process only uses indexOf and substring, which are native methods for dealing with strings, and no regularities are used. It's surprisingly fast, probably close to the internal binary implementation, and has performed well on IE and Firefox (and there is no doubt about it in other browsers). The speed is zero millisecond.
Implement 11String.prototype.trim = function () {var str = this, str = str.replace (/ ^\ s impulse,''); for (var I = str.length-1; I > = 0; imuri -) {if (/\ S/.test (str.charAt (I) {str = str.substring (0, I + 1); break;}} return str;}
Implementation 10 has told us that the normal method of native string interception is much better than regular substitution, although it is a little more complex. But as long as the rules are not too complex, we can use the browser to optimize the rules to improve the efficiency of program execution, such as the performance of 8 in IE. I don't think anyone will usually apply implementation 10 in a project because that whitespace implementation is too long and hard to remember (of course, if you're building a class library, it's definitely the first one). Implementation 11 can be described as its improved version, the white space in the front part is cut by regular replacement, and the native method is used to deal with it later, the effect is not inferior to that of the original, but the speed is very inverse.
Implement 12String.prototype.trim = function () {var str = this, str = str.replace (/ ^\ s\ s), ws = /\ s, I = str.length;while (ws.test (--I)); return str.slice (0, I + 1);}
The better improved version of implementation 10 and implementation 11 in writing, note that it is not about performance speed, but about easy to remember and use. And its two predecessors are zero millisecond level, and will use this to work and scare people later.
The following is the comparison result given by the foreigner. the background of the execution is the trim operation of the article Magna Carta (more than 27600 characters).
To achieve Firefox 2IE 6trim115ms < 0.5mstrim231ms < 0.5mstrim346ms31mstrim447ms46mstrim5156ms1656mstrim6172ms2406mstrim7172ms1640mstrim8281ms < 0.5mstrim9125ms78mstrim10 < 0.5ms < 0.5mstrim11 < 0.5ms < 0.5mstrim12 < 0.5ms < 0.5ms, I believe that everyone has a deeper understanding of the "JavaScript trim function implementation methods", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.