array traversal recommended through
for(var i = 0; i < ar.length; i++) { console.log(ar[i]); } but this approach does not provide for holes in the array.
If there are deleted elements in the array, there is such a processing method:
for(var o of ar) { console.log(o); } but processing speed drops
I have 1 000 000 records. The difference in methods gave a 20-fold slowdown in processing.
var ar = new Array(1000000); ar.fill(1); console.time(); for(var i = 0; i < ar.length; i++) { ar[i] = 2; } console.timeEnd(); console.time(); for(var o of ar) { o = 2; } console.timeEnd(); maybe there is a third way that gives good speed and the necessary flexibility in processing large arrays?
UPDATE
The proposed Леша Марченковский version gives a good speed increase on holey arrays:
var br = new Array(10000000); for(var i = 0; i < 1000000; i++){ var l = parseInt(Math.random() * 1000000); br[i] = 1; } console.time(); for(var i = 0; i < br.length; i++) { if(br[i] == undefined) continue; br[i] = 2; } console.timeEnd(); console.time(); for(var o of br) { o = 2; } console.timeEnd(); default: 1365.980ms
default: 6656.250ms