您好,欢迎访问一九零五行业门户网

Prototype源码浅析 String部分(一)之有关indexOf优化_prototype

添加到string.prototype中的方法比较多,不过归结起来,大致分为下面几类:
分类 方法名 
原始能力增强               strip |  include  |  startswith  |  endswith |  empty |  blank
格式 camelize | capitalize |  underscore |  dasherize  | inspect          
变形 toarray |  succ  | times
替换 interpolate  | sub |  scan |  truncate | gsub
html处理 striptags  | escapehtml |  unescapehtml
参数序列化 toqueryparams
json处理 unfilterjson |  isjson |  evaljson |  parsejson
脚本处理 stripscripts |  extractscripts  | evalscripts
从基本的原始能力增强开始,下面是具体的实现,这一段很好理解的:
复制代码 代码如下:
(function(s){
function strip(){
return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
function include(pattern){
return this.indexof(pattern) > -1;//split
}
function startswith(pattern) {
return this.lastindexof(pattern, 0) === 0;
}
function endswith(pattern) {
var d = this.length - pattern.length;
return d >= 0 && this.indexof(pattern, d) === d;
}
function empty() {
return this == '';
}
function blank() {
return /^\s*$/.test(this);
}
s.strip = string.prototype.trim || strip;
s.include = include;
s.startswith = startswith;
s.endswith = endswith;
s.empty = empty;
s.blank = blank;
})(string.prototype);
上面的strip在jquery里面是$.trim,而且大部分貌似都是trim。这里直接扩展原生原型的悲剧之处就显现出来了,因为后面的js实现中(比如chrome)就实现了trim方法,那就弄巧成拙了。
复制代码 代码如下:
function strip(){
return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
这里面的replace(/^\s+/,'')就是trimleft,replace(/\s+$/,'')是trimright,不过prototype.string中没有这两个方法。
下面是这一部分比较有意思的地方:
当时看这段的时候,对其中的startswith和endswith甚是不解,按理来说,startswith用indexof就可以了,这里却是用的lastindexof。后来去翻了一下prototype1.6版本的实现:
复制代码 代码如下:
function startswith(pattern) {
return this.indexof(pattern) === 0;
}
function endswith(pattern) {
var d = this.length - pattern.length;
return d >= 0 && this.lastindexof(pattern) === d;
}
可见,以前版本中startswith用的就是indexof,不过1.7版本修改了startswith的实现。在1.7版本中:
startswith实现中lastindexof从后向前查找,不过起点(fromindex)设置为0,因此,只需要检测开头一次就可以了。
endswith实现中indexof从前向后查找,由于字符串长度不定,因此这里计算了一下长度,然后再确定了起点(fromindex),因此也只需要检测结尾一次就可以了。
这里的性能优化之处在于,1.6的实现中,如果开头没有匹配(就是startswith不成立),但是indexof依旧会向后查找,直到找到一个匹配的或者字符串结尾,这样就浪费了。举个例子,对于下面的一个操作:
'abcdefgabcdefg'.startswith('abc')
在1.6版本和1.7版本的实现中,没有任何区别,但是我们转换一下:
'abcdefgabcdefg'.startswith('xesam')
在1.6实现中,startswith内部的indexof操作会在开头的a没有和x匹配后,虽然没有必要再继续了,但是indexof依旧会继续向后查找,直到找到匹配的‘xesam'或者字符串末尾。
在1.7实现中,startswith内部的lastindexof是反向查找的(fromindex=0),因此在开头的a没有和x匹配后,操作就停止了,因为lastindexof已经到头了。
这么一对比,如果待检测的字符串非常长的话,两种实现方式的效率会有明显的区别。
endswith的原理也是一样的。
其它类似信息

推荐信息