我想从此URL下载几个数据文件:https : //pselookup.vrymel.com/
该站点包含一个日期字段和一个下载按钮。我想下载数据多年(这将意味着很多请求),并且我想自动进行下载。
我创建了一个Javascript代码段,但是它会不断下载相同的文件。
$dateField = document.getElementsByClassName('csv_download_input__Input-encwx-1 dDiqPH')[2]
$dlButton = document.getElementsByClassName('csv_download_input__Button-encwx-0 KLfyv')[2]
var now = new Date();
var daysOfYear = [];
for (var d = new Date(2016, 0, 1); d <= now; d.setDate(d.getDate() + 1)) {
daysOfYear.push(new Date(d).toISOString().substring(0,10));
}
(function theLoop (i) {
setTimeout(function () {
$dlButton.click()
$dateField.value = daysOfYear[i]
if (--i) { // If i > 0, keep going
theLoop(i); // Call the loop again, and pass it the current value of i
}
}, 3000);
})(daysOfYear.length-1); …Run Code Online (Sandbox Code Playgroud) 这是将数组的值恢复为简单值true或false值的最佳方法。
作为jsperf是给我,我很困惑非常比谷歌浏览器控制台的NodeJS,或任何其他JS引擎带给我不同的结果。(此处为jsperf片段)
这是代码段,您可以看到(可以在此处运行)some比使用foreach循环快100倍
var array = [];
var i = 0;
var flag = false;
while (i< 100000) {
array.push(Math.random()*10000);
i++;
}
console.time('forEach');
array.forEach((item) => {
if (!flag && item > 10000/2) {
flag = true;
return;
}
return false
});
console.timeEnd('forEach');
console.log(flag);
flag = false;
console.time('some');
flag = array.some((item) => {
if (item > 10000/2) {
return true;
}
return false
});
console.timeEnd('some');
console.log(flag);Run Code Online (Sandbox Code Playgroud)
问题是,为什么JSPERF给出的结果与chrome的控制台,nodejs或任何其他JS引擎不同?
编辑:正如我对下面问题的回答所指出的那样,此行为是有问题的,因为 …
在JavaScript中给出这两个函数:
function (){
return _.includes(someLongArray, value) && someSimpleValue;
}
Run Code Online (Sandbox Code Playgroud)
VS
function (){
return someSimpleValue && _.includes(someLongArray, value);
}
Run Code Online (Sandbox Code Playgroud)
哪些会有更好的表现?如果someSimpleValue为false,是否会自动调用返回?或者它是要验证第二个是真还是假(无用因为当&&有一些是假的时候它不再是真的)
javascript ×3
angularjs ×1
arrays ×1
automation ×1
download ×1
jsperf ×1
node.js ×1
performance ×1