因此,我编写了(由于缺乏答案+提高我的技能)一个搜索脚本,该脚本基本上可以完成.indexOf所做的操作。
function search(ref, data) {
var x
var y
var result = []
if (data == '' || data == null) {
} else {
for (x = 0; x < data.length; x++) {
if (data[x] == ref[0]) { //achando match inicial
var proto = [];
for (y = 0; y < ref.length; y++) {
if (data[x+y] == ref[y]) { //gravando tentativas de match completo
proto.push(data[x+y])
}
}
var proto2 = proto.join('')
if (proto2 == ref) { //testando match completo
result.push(x)
}
}
}
}
if (result == '' || result == null) {
} else {
return result[0]
}
}
它在其他不需要太多循环的小代码和自定义函数中工作正常,但是当我编写一个更强大的脚本时,我发现我的代码比本机.indeOf慢大约3000倍。
为什么我会产生这样的差额?
繁华开满天机
相关分类