`
ollevere
  • 浏览: 265645 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

js_trim()十二种实现

    博客分类:
  • JS
 
阅读更多

The analysis
Although there are 11 rows in the table above, they are only the most notable (for various reasons) of about 20 versions I wrote and benchmarked against various types of strings. The following analysis is based on testing in Firefox 2.0.0.4, although I have noted where there are major differences in IE6.

1.
-----------------------------------------------------------------
//return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');

function trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');

}
-------------------------------------------------------------------
All things considered, this is probably the best all-around approach. Its speed advantage is most notable with long strings — when efficiency matters. The speed is largely due to a number of optimizations internal to JavaScript regex interpreters which the two discrete regexes here trigger. Specifically, the pre-check of required character and start of string anchor optimizations, possibly among others.

2.
-------------------------------------------------------------------
return str.replace(/^\s+/, '').replace(/\s+$/, '');
-------------------------------------------------------------------
Very similar to trim1 (above), but a little slower since it doesn't trigger all of the same optimizations.

3.
-------------------------------------------------------------------
return str.substring(Math.max(str.search(/\S/), 0), str.search(/\S\s*$/) + 1);
-------------------------------------------------------------------
This is often faster than the following methods, but slower than the above two. Its speed comes from its use of simple, character-index lookups.

4.
-------------------------------------------------------------------
return str.replace(/^\s+|\s+$/g, '');
-------------------------------------------------------------------
This commonly thought up approach is easily the most frequently used in JavaScript libraries today. It is generally the fastest implementation of the bunch only when working with short strings which don't include leading or trailing whitespace. This minor advantage is due in part to the initial-character discrimination optimization it triggers. While this is a relatively decent performer, it's slower than the three methods above when working with longer strings, because the top-level alternation prevents a number of optimizations which could otherwise kick in.

5.
-------------------------------------------------------------------
str = str.match(/\S+(?:\s+\S+)*/);
return str ? str[0] : '';
-------------------------------------------------------------------
This is generally the fastest method when working with empty or whitespace-only strings, due to the pre-check of required character optimization it triggers. Note: In IE6, this can be quite slow when working with longer strings.

6.
-------------------------------------------------------------------
return str.replace(/^\s*(\S*(\s+\S+)*)\s*$/, '$1');
-------------------------------------------------------------------
This is a relatively common approach, popularized in part by some leading JavaScripters. It's similar in approach (but inferior) to trim8. There's no good reason to use this in JavaScript, especially since it can be very slow in IE6.

7.
-------------------------------------------------------------------
return str.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/, '$1');
-------------------------------------------------------------------
The same as trim6, but a bit faster due to the use of a non-capturing group (which doesn't work in IE 5.0 and lower). Again, this can be slow in IE6.


8.
-------------------------------------------------------------------
return str.replace(/^\s*((?:[\S\s]*\S)?)\s*$/, '$1');
-------------------------------------------------------------------
This uses a simple, single-pass, greedy approach. In IE6, this is crazy fast! The performance difference indicates that IE has superior optimization for quantification of "any character" tokens.

9.
-------------------------------------------------------------------
return str.replace(/^\s*([\S\s]*?)\s*$/, '$1');
-------------------------------------------------------------------
This is generally the fastest with very short strings which contain both non-space characters and edge whitespace. This minor advantage is due to the simple, single-pass, lazy approach it uses. Like trim8, this is significantly faster in IE6 than Firefox 2.
Since I've seen the following additional implementation in one library, I'll include it here as a warning:
-------------------------------------------------------------------
return str.replace(/^\s*([\S\s]*)\b\s*$/, '$1');
-------------------------------------------------------------------
Although the above is sometimes the fastest method when working with short strings which contain both non-space characters and edge whitespace, it performs very poorly with long strings which contain numerous word boundaries, and it's terrible (!) with long strings comprised of nothing but whitespace, since that triggers an exponentially increasing amount of backtracking. Do not use.

A different endgame
There are two methods in the table at the top of this post which haven't been covered yet. For those, I've used a non-regex and hybrid approach.

After comparing and analyzing all of the above, I wondered how an implementation which used no regular expressions would perform. Here's what I tried:

-------------------------------------------------------------------
function trim10 (str) {
 var 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; i < str.length; 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 : '';
}
-------------------------------------------------------------------
How does that perform? Well, with long strings which do not contain excessive leading or trailing whitespace, it blows away the competition (except against trim1/2/8 in IE, which are already insanely fast there).

Does that mean regular expressions are slow in Firefox? No, not at all. The issue here is that although regexes are very well suited for trimming leading whitespace, apart from the .NET library (which offers a somewhat-mysterious "backwards matching" mode), they don't really provide a method to jump to the end of a string without even considering previous characters. However, the non-regex-reliant trim10 function does just that, with the second loop working backwards from the end of the string until it finds a non-whitespace character.

Knowing that, what if we created a hybrid implementation which combined a regex's universal efficiency at trimming leading whitespace with the alternative method's speed at removing trailing characters?

-------------------------------------------------------------------
function trim11 (str) {
 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;
}
-------------------------------------------------------------------
Although the above is a bit slower than trim10 with some strings, it uses significantly less code and is still lightning fast. Plus, with strings which contain a lot of leading whitespace (which includes strings comprised of nothing but whitespace), it's much faster than trim10.

In conclusion…


Since the differences between the implementations cross-browser and when used with different data are both complex and nuanced (none of them are faster than all the others with any data you can throw at it), here are my general recommendations for a trim method:

 

Use trim1 if you want a general-purpose implementation which is fast cross-browser.


Use trim11 if you want to handle long strings exceptionally fast in all browsers.


To test all of the above implementations for yourself, try my very rudimentary benchmarking page. Background processing can cause the results to be severely skewed, so run the test a number of times (regardless of how many iterations you specify) and only consider the fastest results (since averaging the cost of background interference is not very enlightening).

 

As a final note, although some people like to cache regular expressions (e.g. using global variables) so they can be used repeatedly without recompilation, IMO this does not make much sense for a trim method. All of the above regexes are so simple that they typically take no more than a nanosecond to compile. Additionally, some browsers automatically cache the most recently used regexes, so a typical loop which uses trim and doesn't contain a bunch of other regexes might not encounter recompilation anyway.


--------------------------------------------------------------------------------

Edit (2008-02-04): Shortly after posting this I realized trim10/11 could be better written. Several people have also posted improved versions in the comments. Here's what I use now, which takes the trim11-style hybrid approach:
-------------------------------------------------------------------
function trim12 (str) {
 var str = str.replace(/^\s\s*/, ''),
  ws = /\s/,
  i = str.length;
 while (ws.test(str.charAt(--i)));
 return str.slice(0, i + 1);
}
-------------------------------------------------------------------

分享到:
评论

相关推荐

    js_trim()十二种实现.txt

    ### js_trim()十二种实现详解 #### 概述 在前端开发中,字符串处理是非常常见的需求之一,其中去除字符串首尾空白字符的功能尤为重要。本文将详细介绍十二种不同的`js_trim()`实现方法,并对每种方法的性能进行...

    Js_trim.rar_Windows编程_HTML_

    在给定的"Js_trim.rar"压缩包中,包含了一个名为"Js_trim.htm"的文件,很可能这个文件是关于如何使用正则表达式在JavaScript中实现`trim`功能的一个示例或教程。在HTML上下文中,JavaScript常常被用来增强页面的交互...

    js 自定义trim去除字符串左右杂质

    js 自定义trim去除字符串左右杂质 在 Java 中,String 类型提供了 trim() 方法来去除字符串左右的空格。然而,在 JavaScript 中,并没有提供类似的方法来去除字符串左右的杂质。因此,我们需要自定义 trim() 方法来...

    javascript自定义trim()方法

    JavaScript作为一种常用的前端开发语言,并未在标准库中提供直接的`trim()`方法。然而,为了方便开发者处理字符串,本文将详细介绍如何在JavaScript中自定义实现`trim()`方法。 #### 自定义`trim()`方法 在...

    js trim 函数

    javascript 的 trim 函数的实现,去掉字符串的左右空格,由于只使用了String的原生函数,没有使用正则,运算速度极快,是0毫秒级别的,推荐大家使用.

    Javascript中实现trim函数的两种方法.docx

    ### JavaScript中实现trim函数的两种方法 在日常的前端开发工作中,我们经常需要对字符串进行操作,其中一种常见的需求就是去除字符串两端的空白字符。尽管现代JavaScript已经内置了`trim()`方法来帮助开发者轻松...

    js-trim()方法IE无效的解决办法

    ### js-trim()方法IE无效的解决办法 在JavaScript中,`trim()`方法主要用于去除字符串首尾的空白字符。此方法在现代浏览器中通常能够很好地工作,但在Internet Explorer(IE)的一些版本中可能存在兼容性问题。这是...

    helloworld_js_hello_js_

    ”实现的代码示例或者教程,而"sample code with methods for js language"的描述则表明这个压缩包包含了一些JavaScript方法的实践代码。 JavaScript是一种广泛用于Web开发的动态编程语言,主要用途是在浏览器端...

    js设置兼容trim函数

    ### js设置兼容trim函数 #### 知识点一:JavaScript中的`trim()`方法 `trim()` 方法用于删除字符串两端的空白字符(包括空格、制表符、换页符等)。此方法不会改变原始字符串,而是返回一个新的字符串。 **语法**...

    js代码-trim方法实现

    JavaScript中的`trim()`方法是一个非常实用的字符串处理函数,它用于去除字符串两端的空白字符,如空格、换行符等。在JavaScript编程中,处理用户输入或从API获取的数据时,经常需要对字符串进行清洗,`trim()`方法...

    Javascript中实现trim()函数的两种方法

    在JavaScript中我们需要用到trim的地方很多,但是JavaScript又没有独立的trim函数或者方法可以使用,所以我们需要自己写个trim函数来实现我们的目的。 方案一: 以原型方式调用,即obj.trim()形式,此方式简单且使用...

    JavaScript_字符串验证.zip

    JavaScript是一种广泛应用于Web开发的脚本语言,主要在客户端运行,为用户提供动态、交互式的网页体验。它在处理用户输入、验证数据、控制浏览器行为、实现异步通信等方面发挥着关键作用。在这个“JavaScript_字符串...

    javascript的trim和日期时间格式化函数

    ### JavaScript中的Trim函数和日期时间格式化函数 在JavaScript中,字符串处理和日期操作是非常常见的需求,尤其是在数据清洗、格式转换等场景下。本文将详细介绍如何利用自定义方法实现字符串的Trim功能以及日期...

    一个更高效的JavaScript版trim函数.rar

    这个压缩包文件“一个更高效的JavaScript版trim函数.rar”可能包含了一个优化过的实现,旨在提高`trim()`功能的性能。 JavaScript中的原生`trim()`函数是通过正则表达式来实现的,这可能导致在某些浏览器或环境下...

    Js里面给String添加trim()方法,实现去掉字符串两边空格

    根据给定文件中的代码示例,我们可以看到作者通过扩展`String.prototype`,实现了三种不同的字符串修剪方法:`Trim()`, `LTrim()`, 和 `Rtrim()`。下面将详细解析这些方法的实现原理及使用场景。 ### 1. `String....

    js_表单_验证_

    JavaScript是一种常用的客户端脚本语言,可以在用户的浏览器上运行,实时检查用户输入的有效性,而无需等待服务器响应。 一、表单验证的基本概念 表单是HTML中的一个重要元素,用于收集用户输入。验证则是对用户在...

    Javascript中各种trim的实现详细解析

    接着,我们看到JQuery、Mootools、Prototype框架下的trim实现: ```javascript function trim1(str) { return str.replace(/^(\s|\xA0)+|(\s|\xA0)+$/g, ''); } ``` 这里的正则表达式使用了`\xA0`来匹配不间断...

    js自定义trim函数实现删除两端空格功能

    本文实例讲述了js自定义trim函数实现删除两端空格功能。分享给大家供大家参考,具体如下: 兼容IE低版本浏览器,以及其他一些低版本脚本的浏览器 js中本身是没有trim函数的 //删除左右两端的空格 function trim(str...

Global site tag (gtag.js) - Google Analytics