|
|
|
|
|
by Rygian
1689 days ago
|
|
If the array is implemented internally as a map, then the forEach method on t1 already knows it has only two items to process. You can show that as follows: t0 = new Date();
a = Array(100000000).fill(0);
a[100000] = 1;
a[10000000] = 1;
counter = 0;
a.forEach(e => e == 1 ? console.log(e) : counter++);
console.log("elapsed: ", new Date() - t0); // 19415
console.log("visited but not logged: ", counter); // 99999998
t1 = new Date();
a = Array(100000000);
a[100000] = 1;
a[10000000] = 1;
counter = 0;
a.forEach(e => e == 1 ? console.log(e) : counter++);
console.log("elapsed: ", new Date() - t1); // 1936
console.log("visited but not logged: ", counter); // 0
|
|