Решил сравнить, что быстрее работает: цикл for или forEach.
Написал такой тест:
let time = 10000000;
let test1 = () =>{
let a1 = 0;
let t1 = performance.now();
for(let i = 0; i < time; i++){
a1++;
}
console.log(`Time: ${(performance.now()-t1)/1000} seconds`); // Time: 0.036104999999981374 seconds
}
let test2 = () =>{
let a1 = 0;
let t1 = performance.now();
Array.from({length:time}, _=>0).forEach(_=>a1++);
console.log(`Time: ${(performance.now()-t1)/1000} seconds`); // Time: 1.7714999999997671 seconds
}
console.clear();
test1();
test2();
Получается цикл for быстрее, чем forEach.
Вопрос: мой способ тестирования правильный или нет?