Javascript 在阵列中查找值的基准测试解决方案
我很好奇,当你想在一系列可能性中找到一个值时,我想测试哪个函数最快 我通过在Google Chrome控制台中编写一些代码来完成这项研究 下面是我想要比较的函数:(X | | Y | | Z…),array.includes,array.indexOf,regex.test,array.find。在我的示例中,我将使用前两个函数 以下是我的设置:Javascript 在阵列中查找值的基准测试解决方案,javascript,google-chrome,garbage-collection,benchmarking,v8,Javascript,Google Chrome,Garbage Collection,Benchmarking,V8,我很好奇,当你想在一系列可能性中找到一个值时,我想测试哪个函数最快 我通过在Google Chrome控制台中编写一些代码来完成这项研究 下面是我想要比较的函数:(X | | Y | | Z…),array.includes,array.indexOf,regex.test,array.find。在我的示例中,我将使用前两个函数 以下是我的设置: var array = [181, 182, 183, 184, 185, 190]; function orLogicSolution(q) {
var array = [181, 182, 183, 184, 185, 190];
function orLogicSolution(q) {
return q === 181 || q === 182 || q === 183 || q === 184 || q === 185 || q === 190;
}
function includesSolution(q) {
return array.includes(q);
}
function BENCHMARK() {
this.number = 6000000;
var items = [];
for (var j = 0; j < this.number; j++) {
items.push(parseInt(Math.random() * 1000, 10));
}
this.items = items;
}
BENCHMARK.prototype.solve = function (testedFunction) {
var repetition = 10;
var result = 0;
var i, k, then, now;
for (k = 0; k < repetition; k++) {
then = Date.now();
for (i = 0; i < this.number; i++) {
testedFunction(this.items[i]);
}
now = Date.now();
result += now - then;
}
return Math.round(result / repetition);
};
我的结果是:
包含解决方案4ms
奥洛基溶液41ms
然后,我刷新了浏览器,再次复制/粘贴了我的设置,但这次我通过执行相反的操作启动了测试:
console.log('orLogicSolution ' + new BENCHMARK().solve(orLogicSolution) + 'ms');
console.log('includesSolution ' + new BENCHMARK().solve(includesSolution) + 'ms');
我的结果是:
奥洛基溶液4ms
包含解决方案78ms
这与第一次测试完全矛盾
之后,我尝试使用新的浏览器并再次进行设置:
console.log('orLogicSolution ' + new BENCHMARK().solve(orLogicSolution) + 'ms');
console.log('includesSolution ' + new BENCHMARK().solve(includesSolution) + 'ms');
console.log('orLogicSolution ' + new BENCHMARK().solve(orLogicSolution) + 'ms');
console.log('includesSolution ' + new BENCHMARK().solve(includesSolution) + 'ms');
我的结果是:
奥洛基溶液4ms
包含解决方案78ms
奥洛基溶液38ms
包含解决方案81ms
我在Safari上试过,似乎效果很好:
[日志]orLogicSolution 77ms
[日志]包括解决方案144ms
[日志]Orlogic解决方案75ms
[日志]包括解决方案145ms
我的猜测是,这与Chrome和他的垃圾收集管理或函数内存管理有关
所以我的问题是:为什么会发生这种行为,有没有办法修改代码以获得正确的结果
编辑:
获得正确答案的唯一方法是完全重写solve函数,但我们失去了可伸缩性的意义
console.log('includesSolution ' + new BENCHMARK().solve(includesSolution) + 'ms');
BENCHMARK.prototype.solve = function (testedFunction) {
var repetition = 10;
var result = 0;
var i, k, then, now;
for (k = 0; k < repetition; k++) {
then = Date.now();
for (i = 0; i < this.number; i++) {
testedFunction(this.items[i]);
}
now = Date.now();
result += now - then;
}
return Math.round(result / repetition);
};
console.log('orLogicSolution ' + new BENCHMARK().solve(orLogicSolution) + 'ms');
console.log('includesolution'+newbenchmark().solve(includesolution)+'ms');
BENCHMARK.prototype.solve=函数(testedFunction){
var重复=10;
var结果=0;
var i,k,然后,现在;
对于(k=0;k<重复;k++){
然后=Date.now();
对于(i=0;i
我们得到了这些结果:
包含解决方案3ms
奥洛基溶液4ms
所以是的,我不明白为什么D:
console.log('includesSolution ' + new BENCHMARK().solve(includesSolution) + 'ms');
BENCHMARK.prototype.solve = function (testedFunction) {
var repetition = 10;
var result = 0;
var i, k, then, now;
for (k = 0; k < repetition; k++) {
then = Date.now();
for (i = 0; i < this.number; i++) {
testedFunction(this.items[i]);
}
now = Date.now();
result += now - then;
}
return Math.round(result / repetition);
};
console.log('orLogicSolution ' + new BENCHMARK().solve(orLogicSolution) + 'ms');