Try yourself.
let arr = []; for(x = 0; x < 100000; x++) {arr.push(Math.random());}
function oldFor(d) { aux = []; const t0 = performance.now(); for(x=0; x < d.length; x++) { aux.push(d); } return (performance.now() - t0); }
function eachFor(d) { let aux = []; const t0 = performance.now(); d.forEach(v => { aux.push(v); }); return (performance.now() - t0); }
oldFor(arr); eachFor(arr);
Okay. This is your code in typescript with a 10M length array. The forEach time is double.
I made another for just testing the other loops with a 10M length array.
So... the conclusion is... If you want to loop through big arrays just use the old for loop. And I made an account just for to send this comment.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Try yourself.
let arr = [];
for(x = 0; x < 100000; x++) {arr.push(Math.random());}
function oldFor(d) {
aux = [];
const t0 = performance.now();
for(x=0; x < d.length; x++) {
aux.push(d);
}
return (performance.now() - t0);
}
function eachFor(d) {
let aux = [];
const t0 = performance.now();
d.forEach(v => {
aux.push(v);
});
return (performance.now() - t0);
}
oldFor(arr);
eachFor(arr);
Okay. This is your code in typescript with a 10M length array. The forEach time is double.
I made another for just testing the other loops with a 10M length array.
So... the conclusion is... If you want to loop through big arrays just use the old for loop. And I made an account just for to send this comment.