【发布时间】:2020-05-26 14:25:30
【问题描述】:
所以,我写了一个搜索脚本(因为缺乏答案 + 提高了我的技能),它基本上可以完成 .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 倍。
为什么我会产生这样的差异?
【问题讨论】:
-
“缺乏答案”什么问题?
-
为了正确理解您的问题,关于
I found that my code is roughly 3000x slower than the native .indeOf.,您能否提供比较脚本以及用于复制roughly 3000x slower的示例输入和输出值? -
Rùben,我实际上并没有进行比较值得复制,主要是因为我缺乏测试知识 - 也因为结果的差异。我主要是在寻找对 GAS 内部引擎的深入了解,以了解为什么本机代码甚至比那些简单的脚本运行得更快。无论如何,我会进行结构化比较并稍后分享结果。
-
另外,“缺少答案”与 GAS 原生的搜索字符串方法(例如 .indexOf 或 .contains)有关
标签: javascript performance google-apps-script google-sheets custom-function