假设Jan Juna描述的“性能延迟”,我尝试了这个简单的脚本来检查在吞吐量方面是否有任何差异:
间隔:
const max = 50;
const timer = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 100, 150, 200, 250, 300, 400, 500];
function crono(timer) {
return new Promise(resolve => {
const exit = [];
let i = 0
setInterval(() => i++, timer);
setInterval(() => {
exit.push(i);
i = 0;
if (exit.length === max) {
const sum = exit.reduce((a, b) => (a + b), 0);
const avg = sum / exit.length;
console.log(`${timer} = ${avg}`);
resolve(avg)
}
}, 1000);
});
}
Promise.all(timer.map(crono)).then(process.exit);
超时:
const max = 50;
const timer = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 100, 150, 200, 250, 300, 400, 500];
function crono(timer) {
return new Promise(resolve => {
const exit = [];
let i = 0
const redo = () => {
i++
setTimeout(redo, timer);
}
setInterval(() => {
exit.push(i);
i = 0;
if (exit.length === max) {
const sum = exit.reduce((a, b) => (a + b), 0);
const avg = sum / exit.length;
console.log(`${timer} = ${avg}`);
resolve(x)
}
}, 1000);
redo();
});
}
Promise.all(timer.map(crono)).then(process.exit);
这是 nodejs 8.11 的输出:在吞吐量方面没有差异: